我想重寫下面的查詢,因爲我不想使用如此多的窗口函數。有什麼辦法可以重寫下面的邏輯。到目前爲止,我試圖建立臨時表並使用jonins,但是沒有什麼幫助。查詢重寫邏輯
SELECT a.*
FROM (
SELECT F.DT_KEY AS DT_KEY
,F.COL_KEY AS COL_KEY
,F.PCK_ITM_KEY AS PCK_ITM_KEY
,F.KEY3 AS KEY3
,F.KEY4 AS KEY4
,F.KEY1 AS KEY1
,F.KEY2 AS KEY2
,F.KEY3 AS KEY3
,F.SH_QTY AS SH_QTY
,SUM(F.SLS_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS SLS_QTY
,SUM(F.SLS_RTRN_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS SLS_RTRN_QTY
,SUM(F.PCHSE_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS PCHSE_QTY
,SUM(F.ADJ_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS ADJ_QTY
,SUM(F.SED_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS SED_QTY
,SUM(F.RTV_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS RTV_QTY
,SUM(F.PRN_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS PRN_QTY
,SUM(F.ML_IN_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS ML_IN_QTY
,SUM(F.ML_OUT_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS ML_OUT_QTY
,SUM(F.BK_ML_IN_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS BK_ML_IN_QTY
,SUM(F.BK_ML_OUT_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS BK_ML_OUT_QTY
,SUM(F.INT_COMP_IN_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS INT_COMP_IN_QTY
,SUM(F.INT_COMP_OUT_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS INT_COMP_OUT_QTY
,SUM(F.ML_RCPT_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS ML_RCPT_QTY
,SUM(F.RCPT_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS RCPT_QTY
,SUM(F.STCK_ADJ_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS STCK_ADJ_QTY
,SUM(F.WHSL_SLS_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS WHSL_SLS_QTY
,SUM(F.WH_RTRN_QTY) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS WH_RTRN_QTY
,SUM(F.NET_SLS_AMT) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS NET_SLS_AMT
,SUM(F.NET_WH_SLS_AMT) OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
) AS NET_WH_SLS_AMT
,F.IN_TRNST_QTY AS IN_TRNST_QTY
,F.LST_RCVD_QTY AS LST_RCVD_QTY
,F.LST_SLD_QTY AS LST_SLD_QTY
,MIN(F.FRST_RCVD_DT) OVER (
PARTITION BY F.KEY1
,F.KEY3
) AS FRST_RCVD_DT
,MAX(F.LST_RCVD_DT) OVER (
PARTITION BY F.KEY1
,F.KEY3
) AS LST_RCVD_DT
,MAX(F.LST_LST_RCVD_DT) OVER (
PARTITION BY F.KEY1
,F.KEY3
) AS LST_LST_RCVD_DT
,MIN(F.FRST_SLD_DT) OVER (
PARTITION BY F.KEY1
,F.KEY3
) AS FRST_SLD_DT
,MAX(F.LST_SLD_DT) OVER (
PARTITION BY F.KEY1
,F.KEY3
) AS LST_SLD_DT
,MIN(F.FRST_ML_DT) OVER (
PARTITION BY F.KEY1
,F.KEY3
) AS FRST_ML_DT
,MAX(F.LST_ML_DT) OVER (
PARTITION BY F.KEY1
,F.KEY3
) AS LST_ML_DT
,F.LST_MODFD_DTTM AS LST_MODFD_DTTM
,ROW_NUMBER() OVER (
PARTITION BY F.KEY4
,F.KEY1
,F.KEY2
,F.KEY3
,F.COLE ORDER BY F.DT_KEY DESC
) AS RNK
FROM FCT_ITEM_KEY F
) a
WHERE a.RNK = 1;
這是你**如何工作?你看別人的代碼並開始改變它,而不會問「輸入數據是什麼樣的」,「需求是什麼」以及「輸出應該如何」?如果是這樣,我不想讓你處理我的代碼。如果沒有(很可能),爲什麼你期望我們這樣工作?請說明輸入內容,要求是什麼以及輸出應該是什麼。不要讓我們通過閱讀代碼來「猜測」 - 這不是應該如何完成的。 – mathguy
@mathguy我對這個論壇很新。感謝您的建議,我將包含示例數據。 – ciprian
嗨Ciprian, - 你可能會覺得這很有用(當你有時間的話)。 http://stackoverflow.com/help/特別是http://stackoverflow.com/help/mcve – mathguy