2017-01-28 139 views
0

我想重寫下面的查詢,因爲我不想使用如此多的窗口函數。有什麼辦法可以重寫下面的邏輯。到目前爲止,我試圖建立臨時表並使用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; 
+0

這是你**如何工作?你看別人的代碼並開始改變它,而不會問「輸入數據是什麼樣的」,「需求是什麼」以及「輸出應該如何」?如果是這樣,我不想讓你處理我的代碼。如果沒有(很可能),爲什麼你期望我們這樣工作?請說明輸入內容,要求是什麼以及輸出應該是什麼。不要讓我們通過閱讀代碼來「猜測」 - 這不是應該如何完成的。 – mathguy

+0

@mathguy我對這個論壇很新。感謝您的建議,我將包含示例數據。 – ciprian

+0

嗨Ciprian, - 你可能會覺得這很有用(當你有時間的話)。 http://stackoverflow.com/help/特別是http://stackoverflow.com/help/mcve – mathguy

回答

0

我想你可以使用條件的聚集,如果你喜歡:

SELECT F.KEY4, F.KEY1, F.KEY2, F.KEY3, F.COLE, 
     MAX(CASE WHEN seqnum = 1 THEN F.DT_KEY END) AS DT_KEY, 
     MAX(CASE WHEN seqnum = 1 THEN F.COL_KEY END) AS COL_KEY, 
     MAX(CASE WHEN seqnum = 1 THEN F.PCK_ITM_KEY END) AS PCK_ITM_KEY, 
     MAX(CASE WHEN seqnum = 1 THEN F.SH_QTY END) AS SH_QTY, 
     SUM(F.SLS_QTY) AS SLS_QTY, 
     . . . -- similar for all the sums 
     MIN(MIN(F.FRST_RCVD_DT)) OVER (PARTITION BY F.KEY1, F.KEY3) AS FRST_RCVD_DT, -- still need the window function because the keys are a subset of the `group by` keys, 
     . . . 
FROM (SELECT F.*, 
      ROW_NUMBER() OVER (PARTITION BY F.KEY4, F.KEY1, F.KEY2, F.KEY3, F.COLE 
           ORDER BY F.DT_KEY DESC 
          ) AS seqnum 
     FROM FCT_ITEM_KEY F 
    ) F 
GROUP BY F.KEY4, F.KEY1, F.KEY2, F.KEY3, F.COLE; 

我不知道如果表現會更好。

+0

感謝這個解決方案,它執行正常,除了'MIN(MIN(F.FRST_RCVD_DT))OVER(PARTITION BY F.KEY1 ,F.KEY3)作爲FRST_RCVD_DT'和相關的。由於哪個執行時間增加了比原來的更多。任何解決方法可行? – ciprian

+0

@ciprian如果您遇到性能問題,您是否可以共享SQL Monitor報告 – BobC

+0

在KEY1,KEY2,KEY3,KEY4上構建索引後,性能提高很多。 – ciprian