将一列转换为其他列的列标题

时间:2019-01-29 12:15:46

标签: oracle11g pivot

我仍在努力将自己的头包裹在Pivot和Unpivot周围,并希望得到我想要的结果, 但我不知道如何获得我的欲望输出。所以我希望你们中的ORACLE / T-SQL专家可以在这里为我提供帮助。

我的查询目标

将一列转换为其他列的列标题(其他列将嵌套在多列下,这是列标题)

在这里,我已经提供了有关我的问题,预期结果以及我正在尝试什么的所有信息

表格脚本:

CREATE TABLE LC_PERFORMANCE_O
(PERIOD VARCHAR2(100), LC_NUMBER VARCHAR2(20), LC_VALUE NUMBER)

插入脚本:

SET DEFINE OFF;
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('JANUARY10', 69, 5282964.68, '01');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('FEBRUARY10', 57, 4345701.5, '02');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('MARCH10', 63, 2570537.13, '03');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('APRIL10', 51, 2003928.9, '04');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('MAY10', 62, 2729072.07, '05');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('JUNE10', 51, 5455665.58, '06');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('JULY10', 55, 4499081.21, '07');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('AUGUST10', 55, 2493876.83, '08');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('SEPTEMBER10', 68, 3203650.57, '09');
Insert into LC_PERFORMANCE
   (PERIOD, LC_NUMBER, LC_VALUE, PERIOD_NO)
 Values
   ('OCTOBER10', 107, 7795475.07, '10');
COMMIT;

输入:LC_PERFORMANCE表

+-------------+-----------+------------+-----------+
| PERIOD      | LC_NUMBER | LC_VALUE   | PERIOD_NO |
+-------------+-----------+------------+-----------+
| JANUARY10   | 69        | 5282964.68 | 01        |
+-------------+-----------+------------+-----------+
| FEBRUARY10  | 57        | 4345701.5  | 02        |
+-------------+-----------+------------+-----------+
| MARCH10     | 63        | 2570537.13 | 03        |
+-------------+-----------+------------+-----------+
| APRIL10     | 51        | 2003928.9  | 04        |
+-------------+-----------+------------+-----------+
| MAY10       | 62        | 2729072.07 | 05        |
+-------------+-----------+------------+-----------+
| JUNE10      | 51        | 5455665.58 | 06        |
+-------------+-----------+------------+-----------+
| JULY10      | 55        | 4499081.21 | 07        |
+-------------+-----------+------------+-----------+
| AUGUST10    | 55        | 2493876.83 | 08        |
+-------------+-----------+------------+-----------+
| SEPTEMBER10 | 68        | 3203650.57 | 09        |
+-------------+-----------+------------+-----------+

预期的输出:来自数据库视图

+-----------------+----------------+-----------------+----------------+-----------------+-----------------+-----------------+
|   JANUARY10     |  FEBRUARY10    |    MARCH10      |    APRIL10     |     MAY10       |     JUNE10      | Continuous year |
+-----------------+----------------+-----------------+----------------+-----------------+-----------------+-----------------+
| 69 | 5282964.68 | 57 | 4345701.5 | 63 | 2570537.13 | 51 | 2003928.9 | 62 | 2729072.07 | 51 | 5455665.58 | ...    | ...    |
+----+------------+----+-----------+----+------------+----+-----------+----+------------+----+------------+--------+--------+

我的查询:

CLEAR COLUMNS
COLUMN TEMP_IN_STATEMENT NEW_VALUE STR_IN_STATEMENT;

/

SELECT   DISTINCT
         LISTAGG ('''' || PERIOD || ''' AS ' || PERIOD, ',')
            WITHIN GROUP (ORDER BY PERIOD_NO)
            AS TEMP_IN_STATEMENT
  FROM   (SELECT   DISTINCT PERIOD, PERIOD_NO FROM IMPORT_LC_PERFORMANCE);

/

SELECT * FROM
(
SELECT * FROM
    (SELECT PERIOD,LC_VALUE FROM IMPORT_LC_PERFORMANCE)
    PIVOT (MAX( LC_VALUE  ) FOR PERIOD IN (&STR_IN_STATEMENT))
    UNION
SELECT * FROM
    (SELECT PERIOD,LC_NUMBER FROM IMPORT_LC_PERFORMANCE)
    PIVOT (MAX( LC_NUMBER  ) FOR PERIOD IN (&STR_IN_STATEMENT))
    )T

针对我的查询的结果:

+------------+------------+------------+-----------+------------+------------+------------+------------+-------------+------------+
| JANUARY10  | FEBRUARY10 | MARCH10    | APRIL10   | MAY10      | JUNE10     | JULY10     | AUGUST10   | SEPTEMBER10 | OCTOBER10  |
+------------+------------+------------+-----------+------------+------------+------------+------------+-------------+------------+
| 69         | 57         | 63         | 51        | 62         | 51         | 55         | 55         | 68          | 107        |
+------------+------------+------------+-----------+------------+------------+------------+------------+-------------+------------+
| 5282964.68 | 4345701.5  | 2570537.13 | 2003928.9 | 2729072.07 | 5455665.58 | 4499081.21 | 2493876.83 | 3203650.57  | 7795475.07 |
+------------+------------+------------+-----------+------------+------------+------------+------------+-------------+------------+

0 个答案:

没有答案