我正在尝试重写下面的查询,因为我不想使用这么多窗口函数。有没有办法重写下面的逻辑。到目前为止,我已经尝试构建临时表并使用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 :(得分: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;
我不知道表现是否会更好。