ORACLE PIVOT QUERY 10G

时间:2013-10-24 15:14:56

标签: oracle oracle10g pivot

要求是以数据透视表格式重新生成结果。

我们每个月有22个表,每个表有大约150万行。所需的输出是每个产品/类别的22个表格保存信息,需要以列格式生成产品

在11g中我们使用了pivot函数来重现和10g我们编写了以下查询。此查询将继续运行1天而不会撤消任何结果,因为最大聚合函数需要花费大量时间来计算。有没有办法重写以下查询。

SELECT year,month,account_id,media_id,status,
        max(CP1) AS                 CP,
        max(CA1) AS                 CA,
        max(CU1) AS                    CU,
        max(GENDER1) AS                  GENDER,
        max(AGE1)  AS                AGE,
        max(AREA1) AS               AREA,
        max(SB1) AS               SB,
        max(SSP1) AS             SSP,
        max(SRP) AS            scheme_redem_prpt,
        max(BAL1) AS         BAL,
        max(PTS_RE1) AS       PTS_RE,
        max(PTS_EA1) AS      PTS_EA,
        max(dept_01_txn1) AS                dept_01_txn,
        max(dept_02_txn1) AS                dept_02_txn,
        max(dept_03_txn1) AS                dept_03_txn,
        max(dept_04_txn1) AS                dept_04_txn,
        max(dept_05_txn1) AS                dept_05_txn,
        max(dept_01_amt1) AS              dept_01_amt,
        max(dept_02_amt1) AS              dept_02_amt,
        max(dept_03_amt1) AS              dept_03_amt,
        max(dept_04_amt1) AS              dept_04_amt,
        max(dept_05_amt1) AS              dept_05_amt
  FROM (SELECT year,month,account_id,media_id,status,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('CR1') , prdatt_value, null),'00') AS CP1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('CR2')  , prdatt_value, null),'00') AS CA1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('CR3')   , prdatt_value, null),'00') AS CU1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('GENDER') , prdatt_value, null),'00') AS GENDER1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('AGE') , prdatt_value, null),'00') AS AGE1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('AREA') , prdatt_value, null),'00') AS AREA1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('SB1') , prdatt_value, null),'00') AS SB1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('SB2') , prdatt_value, null),'00') AS SSP1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('SB3') , prdatt_value, null),'00') AS SRP,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('SR1') , prdatt_value, null),'00') AS BAL1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('SR2') , prdatt_value, null),'00') AS PTS_RE1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('SR3') , prdatt_value, null),'00') AS PTS_EA1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_01_TXN') , prdatt_value, null),'00') AS dept_01_txn1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_02_TXN') , prdatt_value, null),'00') AS dept_02_txn1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_03_TXN') , prdatt_value, null),'00') AS dept_03_txn1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_04_TXN') , prdatt_value, null),'00') AS dept_04_txn1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_05_TXN'), prdatt_value, null),'00') AS dept_05_txn1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_01_AMT') , prdatt_value, null),'00') AS dept_01_amt1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_02_AMT') , prdatt_value, null),'00') AS dept_02_amt1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_03_AMT') , prdatt_value, null),'00') AS dept_03_amt1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_04_AMT') , prdatt_value, null),'00') AS dept_04_amt1,
                nvl(decode(prdatt_id,util_pkg.fnc_get_Value('DEPT_05_AMT') , prdatt_value, null),'00') AS dept_05_amt1
         FROM (
            select year, month, account_id,  media_id,prdatt_id, prdatt_iid, prdatt_value , status 
              from ac_prd_tab_01
            union all
            select year, month, account_id,  media_id,prdatt_id, prdatt_iid, prdatt_value , status 
              from ac_prd_tab_02
            union all
            select year, month, account_id,  media_id,prdatt_id, prdatt_iid, prdatt_value , status 
              from ac_prd_tab_03
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_04 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
              from ac_prd_tab_05 a,
                   dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
              from ac_prd_tab_06 a,
                   dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
              from ac_prd_tab_07 a,
                   dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id
            union all 
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_08 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id 
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_09 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_10 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_11 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id 
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_12 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id 
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_13 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id 
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_14 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_15 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_16 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id 
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_17 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id 
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
              from ac_prd_tab_18 a,
                   dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_19 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_20 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_21 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            union all
            select a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
            from ac_prd_tab_22 a,
                 dtl_monthly b 
            where a.year= b.year and a.month=b.month and a.account_id=b.account_id  
            )
       )
  GROUP BY year,month,account_id, media_id,status
/

1 个答案:

答案 0 :(得分:1)

除了最大值,你还使用了很多功能。

首先取代

   SELECT a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
     FROM ac_prd_tab_21 a,
          dtl_monthly b 
    WHERE a.year= b.year AND a.month=b.month AND a.account_id=b.account_id  
UNION ALL
   SELECT a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
     FROM ac_prd_tab_22 a,
             dtl_monthly b 
    WHERE a.year= b.year AND a.month=b.month AND a.account_id=b.account_id
        ...

我会尝试

   SELECT a.year, a.month, a.account_id, b.media_id , a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
     FROM (

   SELECT a.year, a.month, a.account_id, a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
     FROM ac_prd_tab_21 a
UNION ALL
   SELECT a.year, a.month, a.account_id, a.prdatt_id, a.prdatt_iid, a.prdatt_value , a.status 
     FROM ac_prd_tab_22 a
          )
INNER JOIN dtl_monthly b 
        ON a.year= b.year AND a.month=b.month AND a.account_id=b.account_id  

第二件事,我将尝试在多个步骤中拆分查询,这些步骤可以填充实际表格,也许是在一个过程中。

第三件事,如果您使用某个程序来计算每次util_pkg.fnc_get_Value()的值将它们放入某些变量(如vCR1 := util_pkg.fnc_get_Value('CR1'))时,那么:

nvl(decode(prdatt_id,util_pkg.fnc_get_Value('CR1') , prdatt_value, null),'00')

将成为这个:

nvl(decode(prdatt_id, vCR1, prdatt_value, null),'00')

额外的小事

(decode(prdatt_id, vCR1, nvl(prdatt_value, '00'), '00')

这应该足以开始了。