2013-10-24 127 views
-3

要求是以數據透視表格式重新生成結果。ORACLE PIVOT查詢10G

我們每月有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

讀取您的查詢並不好玩。 – Zane

+1

想象一下吧 – mucio

+2

@mucio我不會寫這個查詢。 – Zane

回答

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') 

這應該是e開始。

+2

+1甚至可以跑步。 – Zane

+0

@ mucio ..感謝您的建議... fyi我試着用max和它的運行良好的查詢....只有我添加到最大,以將這些行轉到cols。意思,而我嘗試運行查詢tiwht我們的建議 – UserKK