📜  在 oracle 中将行转换为列 - SQL (1)

📅  最后修改于: 2023-12-03 15:07:45.124000             🧑  作者: Mango

在 Oracle 中将行转换为列 - SQL

在Oracle中将行转换为列是一种非常常见的操作,也被称为“数据透视”。本文将介绍如何使用SQL在Oracle中进行行列转换,并提供具体的代码示例。

使用PIVOT函数进行行列转换

在Oracle中,可以使用PIVOT函数进行行列转换。假定有以下数据表:

CREATE TABLE sales (
    product  VARCHAR2(50),
    region   VARCHAR2(50),
    quarter  VARCHAR2(50),
    amount   NUMBER
);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'North', 'Q1', 10000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'North', 'Q2', 15000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'North', 'Q3', 12000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'North', 'Q4', 9000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'South', 'Q1', 8000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'South', 'Q2', 10000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'South', 'Q3', 11000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product A', 'South', 'Q4', 12000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'North', 'Q1', 7000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'North', 'Q2', 8000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'North', 'Q3', 9000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'North', 'Q4', 10000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'South', 'Q1', 9000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'South', 'Q2', 7000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'South', 'Q3', 8000);

INSERT INTO sales (product, region, quarter, amount)
VALUES ('Product B', 'South', 'Q4', 6000);

表格如下:

| PRODUCT | REGION | QUARTER | AMOUNT | |-------------|--------|---------|--------| | Product A | North | Q1 | 10000 | | Product A | North | Q2 | 15000 | | Product A | North | Q3 | 12000 | | Product A | North | Q4 | 9000 | | Product A | South | Q1 | 8000 | | Product A | South | Q2 | 10000 | | Product A | South | Q3 | 11000 | | Product A | South | Q4 | 12000 | | Product B | North | Q1 | 7000 | | Product B | North | Q2 | 8000 | | Product B | North | Q3 | 9000 | | Product B | North | Q4 | 10000 | | Product B | South | Q1 | 9000 | | Product B | South | Q2 | 7000 | | Product B | South | Q3 | 8000 | | Product B | South | Q4 | 6000 |

现在我们想将这个数据表转换为以下格式:

| PRODUCT | NORTH_Q1 | NORTH_ Q2 | NORTH_ Q3 | NORTH_Q4 | SOUTH_Q1 | SOUTH_ Q2 | SOUTH_Q3 | SOUTH_Q4 | |-----------|----------|-----------|-----------|----------|----------|-----------|----------|----------| | Product A | 10000 | 15000 | 12000 | 9000 | 8000 | 10000 | 11000 | 12000 | | Product B | 7000 | 8000 | 9000 | 10000 | 9000 | 7000 | 8000 | 6000 |

可以使用如下SQL代码实现:

SELECT *
FROM (
    SELECT product, region || '_' || quarter AS region_quarter, amount
    FROM sales
)
PIVOT (
    MAX(amount)
    FOR region_quarter IN (
        'North_Q1' AS NORTH_Q1,
        'North_Q2' AS NORTH_Q2,
        'North_Q3' AS NORTH_Q3,
        'North_Q4' AS NORTH_Q4,
        'South_Q1' AS SOUTH_Q1,
        'South_Q2' AS SOUTH_Q2,
        'South_Q3' AS SOUTH_Q3,
        'South_Q4' AS SOUTH_Q4
    )
)
ORDER BY product;

上述代码执行结果如下:

| PRODUCT | NORTH_Q1 | NORTH_ Q2 | NORTH_ Q3 | NORTH_Q4 | SOUTH_Q1 | SOUTH_ Q2 | SOUTH_Q3 | SOUTH_Q4 | |-----------|----------|-----------|-----------|----------|----------|-----------|----------|----------| | Product A | 10000 | 15000 | 12000 | 9000 | 8000 | 10000 | 11000 | 12000 | | Product B | 7000 | 8000 | 9000 | 10000 | 9000 | 7000 | 8000 | 6000 |

使用CASE语句进行行列转换

除了使用PIVOT函数进行行列转换,也可以使用CASE语句实现。以下是使用CASE语句实现行列转换的示例代码:

SELECT
    product,
    MAX(CASE WHEN region = 'North' AND quarter = 'Q1' THEN amount END)   AS NORTH_Q1,
    MAX(CASE WHEN region = 'North' AND quarter = 'Q2' THEN amount END)   AS NORTH_Q2,
    MAX(CASE WHEN region = 'North' AND quarter = 'Q3' THEN amount END)   AS NORTH_Q3,
    MAX(CASE WHEN region = 'North' AND quarter = 'Q4' THEN amount END)   AS NORTH_Q4,
    MAX(CASE WHEN region = 'South' AND quarter = 'Q1' THEN amount END)   AS SOUTH_Q1,
    MAX(CASE WHEN region = 'South' AND quarter = 'Q2' THEN amount END)   AS SOUTH_Q2,
    MAX(CASE WHEN region = 'South' AND quarter = 'Q3' THEN amount END)   AS SOUTH_Q3,
    MAX(CASE WHEN region = 'South' AND quarter = 'Q4' THEN amount END)   AS SOUTH_Q4
FROM sales
GROUP BY product;

该代码执行结果与PIVOT函数示例代码所产生的结果相同。

总结

通过使用PIVOT函数和CASE语句,我们可以方便地在Oracle中进行行列转换。这是一种非常实用的技术,可以帮助我们轻松地处理大量的数据,并将其展示在表格中,方便进行数据分析。