要求是以數據透視表格式重新生成結果。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
/
讀取您的查詢並不好玩。 – Zane
想象一下吧 – mucio
@mucio我不會寫這個查詢。 – Zane