数据透视表

时间:2017-08-28 07:18:10

标签: pivot

我是SQL的初级用户,我想将我的数据转移到右边的表'NEW',我查询的当前数据库视图是左表'OLD' enter image description here

OLD

ENGINE_ASOF_CALENDAR_DATE   GROWTH_ACTIVE_EXP   VALUE_ACTIVE_EXP
24/08/2017 0:00                 -0.144960562            0.258170852
23/08/2017 0:00                 -0.143660442            0.258379336
22/08/2017 0:00                 -0.145358176            0.259934845
21/08/2017 0:00                 -0.14478461         0.260095044

NEW

ENGINE_ASOF_CALENDAR_DATE   FACTOR                  VALUE
24/08/2017 0:00                 GROWTH_ACTIVE_EXP   -0.144960562
23/08/2017 0:00                 GROWTH_ACTIVE_EXP   -0.143660442
22/08/2017 0:00                 GROWTH_ACTIVE_EXP   -0.145358176
21/08/2017 0:00                 GROWTH_ACTIVE_EXP   -0.14478461
24/08/2017 0:00                 VALUE_ACTIVE_EXP    0.258170852
23/08/2017 0:00                 VALUE_ACTIVE_EXP    0.258379336
22/08/2017 0:00                 VALUE_ACTIVE_EXP    0.259934845
21/08/2017 0:00                 VALUE_ACTIVE_EXP    0.260095044

1 个答案:

答案 0 :(得分:0)

这里的通用SQL解决方案是使用UNION来构造所需的输出。在这里,我们会根据我们在该记录中选择的值列引入FACTOR标签列(即GROWTH_ACTIVE_EXPVALUE_ACTIVE_EXP

SELECT
    ENGINE_ASOF_CALENDAR_DATE,
    'GROWTH_ACTIVE_EXP' AS FACTOR,
    GROWTH_ACTIVE_EXP AS VALUE
FROM yourTable
UNION ALL
SELECT
    ENGINE_ASOF_CALENDAR_DATE,
    'VALUE_ACTIVE_EXP',
    VALUE_ACTIVE_EXP AS VALUE
FROM yourTable
ORDER BY
    ENGINE_ASOF_CALENDAR_DATE,
    FACTOR