来自事务表

时间:2017-02-06 17:39:53

标签: postgresql

我遇到了一个要求,我需要每周从事务表中汇总以进行报告。 我有一个事实表,其中包含产品数量和转移数量,我有日期维度,其日期键与周密钥映射。

FCT_PROD_TBL:

DT_KEY      PRD_KEY     LOC_KEY SOH_QTY TSF_IN_QTY
20161113    20012       30034       0       2
20161114    20012       30034       0       1
20161115    20012       30034       3       0
20161118    20012       30034       3       1
20161119    20012       30034       4       0
20161214    20012       30034       2       0
20161215    20012       30034       0       0

DIM_DT:

DT_KEY      TRD_WK_KEY
20161113    20161114
20161114    20161114
20161115    20161121
20161116    20161121
20161117    20161121
20161118    20161121
20161119    20161121
20161120    20161121
20161121    20161121
20161122    20161130
20161123    20161130
20161124    20161130
20161125    20161130
20161126    20161130
20161127    20161130
20161128    20161130
20161129    20161130
20161130    20161130
20161201    20161207
20161202    20161207
20161203    20161207
20161204    20161207
20161205    20161207
20161206    20161207
20161207    20161207
20161208    20161214
20161209    20161214
20161210    20161214
20161211    20161214
20161212    20161214
20161213    20161214
20161214    20161214
20161215    20161221
20161216    20161221
20161217    20161221
20161218    20161221
20161219    20161221
20161220    20161221
20161221    20161221
20161222    20161231
20161223    20161231
20161224    20161231
20161225    20161231
20161226    20161231
20161227    20161231
20161228    20161231
20161229    20161231
20161230    20161231
20161231    20161231

需要输出:

TRD_WK_KEY  PRD_KEY     LOC_KEY SOH_QTY TSF_IN_QTY
20161114    20012       30034       0       3
20161121    20012       30034       4       1
20161130    20012       30034       4       0
20161207    20012       30034       4       0
20161214    20012       30034       2       0
20161221    20012       30034       0       0
20161231    20012       30034       0       0

到目前为止,我尝试使用左外连接无法实现所需的输出。如果任何一周都没有发生任何交易,那么它应该复制上一个最新的每周聚合值。任何帮助或提示以获得所需的输出。

SOH_QTY逻辑: SOH_QTY是该日期的最新版本。例如20161114,20161115,2016118,20161119 SOH_QTY是0,3,3,4所以包含上述日期的TRD_WK_KEY应该包含最后一个SOH_QTY即4

2 个答案:

答案 0 :(得分:0)

您在soh_qty中的预期值对我来说不合适。

为此,您可以使用FIRST_VALUE窗口函数,如下所示:

select
    t.trd_wk_key,
    first_value(prd_key) over (partition by grp order by trd_wk_key) prd_key,
    first_value(loc_key) over (partition by grp order by trd_wk_key) loc_key,
    soh_qty,
    tsf_in_qty
from (
    select 
        d.trd_wk_key,
        max(f.prd_key) prd_key,
        max(f.loc_key) loc_key,
        coalesce(sum(f.soh_qty),0) soh_qty,
        coalesce(sum(f.tsf_in_qty),0) tsf_in_qty,
        sum(case when max(f.prd_key) is null then 0 else 1 end) over (order by trd_wk_key) grp
    from DIM_DT d
    left join FCT_PROD_TBL f
    on d.dt_key = f.dt_key
    group by d.trd_wk_key
) t;

答案 1 :(得分:0)

CTE功能,从FCT_PROD_TBL表生成一系列MIN和MAX之间的所有周数,交叉加入所有PRD_KEYLOC_KEY。 (以防你的桌子有多个PRD_KEY, LOC_KEY

第二部分计算SOH_QTYTSF_IN_QTY的和,并按TRD_WK_KEY, PRD_KEY, LOC_KEY对其进行分组。

WITH WEEKS AS
(
    SELECT DIM_DT.DT_KEY, DIM_DT.TRD_WK_KEY, PRD_KEY, LOC_KEY
    FROM DIM_DT
         CROSS JOIN (SELECT DISTINCT PRD_KEY, LOC_KEY FROM FCT_PROD_TBL) A
    WHERE DIM_DT.DT_KEY >= (SELECT MIN(DT_KEY) FROM FCT_PROD_TBL)
          AND DIM_DT.DT_KEY <= (SELECT MAX(DT_KEY) FROM FCT_PROD_TBL)
)    
SELECT WEEKS.TRD_WK_KEY, WEEKS.PRD_KEY, WEEKS.LOC_KEY,
       COALESCE(SUM(SOH_QTY),0,0) AS SOH_QTY,
       COALESCE(SUM(TSF_IN_QTY),0,0) AS TSF_IN_QTY
FROM WEEKS
     LEFT JOIN FCT_PROD_TBL ON FCT_PROD_TBL.DT_KEY = WEEKS.DT_KEY
GROUP BY WEEKS.TRD_WK_KEY, WEEKS.PRD_KEY, WEEKS.LOC_KEY
ORDER BY WEEKS.TRD_WK_KEY, WEEKS.PRD_KEY, WEEKS.LOC_KEY
;

+------------+---------+---------+---------+------------+
| trd_wk_key | prd_key | loc_key | soh_qty | tsf_in_qty |
+------------+---------+---------+---------+------------+
| 20161114   | 20012   | 30034   | 0       | 3          |
+------------+---------+---------+---------+------------+
| 20161121   | 20012   | 30034   | 10      | 1          |
+------------+---------+---------+---------+------------+
| 20161130   | 20012   | 30034   | 0       | 0          |
+------------+---------+---------+---------+------------+
| 20161207   | 20012   | 30034   | 0       | 0          |
+------------+---------+---------+---------+------------+
| 20161214   | 20012   | 30034   | 2       | 0          |
+------------+---------+---------+---------+------------+
| 20161221   | 20012   | 30034   | 0       | 0          |
+------------+---------+---------+---------+------------+

BTW,这个值:

20161115    20012       30034       3       0
20161118    20012       30034       3       1
20161119    20012       30034       4       0

属于第20161121