在Oracle SQL中创建动态数据透视表

时间:2014-12-24 16:41:36

标签: sql oracle dynamic pivot

我目前有一个正在运行的静态数据透视查询。

WITH    ROW_SET    AS
(
SELECT DEFKEY.CF_DEFERRAL_DURATION, KEYACM.CF_KEYWORD_ID, KEYACM.CF_PERIOD, KEYACM.CF_VALUE,
row_number () OVER ( PARTITION BY  KEYACM.CF_KEYWORD_ID
                      ORDER BY      KEYACM.CF_PERIOD desc
           )   AS r_num
           FROM AMEXIV.MAS_CFUS_KEYACM_CONTROLDATA KEYACM
           JOIN AMEXIV.MAS_CFUS_DEFKEY_CONTROLDATA DEFKEY
           ON DEFKEY.CF_MODEL_ID = KEYACM.CF_MODEL_ID
           AND DEFKEY.CF_KEYWORD_ID = CONCAT(KEYACM.CF_KEYWORD_ID, '_accum_deferral')
)
SELECT    *
FROM      ROW_SET
PIVOT     (   MIN (ROW_SET.CF_VALUE)     AS VALUE
          ,   MIN (ROW_SET.CF_PERIOD)  AS PERIOD
          FOR r_num  IN ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
          ) KEYACM

我想转换FOR r_num IN(1,2,3 ......等)行,以便它使用字段DEFKEY.CF_DEFERRAL_DURATION中的值。该字段是计算应该发生的月数,它目前仅对延迟持续时间为12个月的记录进行正确的计算。但还有其他人有6个月。

有没有办法从DEFKEY.CF_DEFERRAL_DURATION字段中读取此值?

0 个答案:

没有答案