資料倉儲-按周的累加和統計月資料

flzhang發表於2015-05-08
公司要出一報表,計算某月資料,並按週資料的累加計算出月的資料,這裡周的劃分是自己規定,比如5.3號屬於4月的18周,不是5月的周,格式類似這樣
model cd,流通型別code,客戶型別code,總重點賣場數,week19%,week19

因此在根據周的累加和統計月的資料方法如下

SELECT  /*+ FULL(T1) FULL(T2) USE_HASH(T1) */
                                T1.BASE_YW
                                ,T_LOCL.BRNC_ID
                                ,MAX(T_LOCL.BRNC_NM) BRNC_ID_NM
                                ,T_LOCL.OFFC_ID
                                ,MAX(T_LOCL.OFFC_NM) OFFC_ID_NM
                                ,T1.SHOP_ID
                                ,T_MODL.MKT_MODL_ID
                                ,T_MODL.BASC_MODL
                                ,T_CHNL.BI_CHNL_TP_02
                                ,MAX(T_BI_CHNL_TP_02.CODE_NM) BI_CHNL_TP_02_NM
                                ,T_CHNL.BI_CHNL_TP_03
                                ,MAX(T_BI_CHNL_TP_03.CODE_NM) BI_CHNL_TP_03_NM
                                ,SUM(PSI_VAL) PSI_VAL
                                ,MAX(T_YW.YW_RNK) YW_RNK
                        FROM    MCS_BI.DM_BI_TPS_SHOP_01_YW T1 --對應規定周
                                ,T_MODL --對應自然月
 /*一 周和自然月的第一週關聯 這裡是5.3號統計資料
5.3號在規定周裡是18周最後一天 4.27-5.3
在自然月裡是5月第一週,前三天 5.1-5.3
這兩個時間段資料連線,能取出5.1-5.3的資料
二 5.8號在規定周裡是19周 5.3-5.10
   在自然月裡是5月第一週 5.1-5.8
這兩個時間段資料連線,能取出5.3-5.8的資料
因此計算19週資料可以先算出19週資料 從5.3開始的資料,然後計算18周的資料截至到5.3號的資料,這樣資料就完整連線上了 */
                                ,T_SHOP
                                ,T_LOCL
                                ,T_REGN
                                ,T_CHNL
                                ,T_YW
                                ,MCS_HQ.CD_CODE_LIST T_BI_CHNL_TP_02
                                ,MCS_HQ.CD_CODE_LIST T_BI_CHNL_TP_03
                        WHERE   T1.BASE_YW >= (SELECT MIN(A.BASE_YW) FROM MCS_HQ.MA_BASE_YW A WHERE A.WEEK_YM = '201504')
                        AND     T1.BASE_YW <= LEAST((SELECT MAX(C.BASE_YW) FROM MCS_HQ.MA_BASE_YW C WHERE C.WEEK_YM = '201505'),(SELECT MAX(C.BASE_YW) FROM MCS_HQ.MA_BASE_YW C WHERE '20150503' BETWEEN C.FR_YMD AND C.TO_YMD))
                        AND     T_YW.BASE_YW = T1.BASE_YW
                        AND     T1.BI_CHNL_PSI_TP = 'I3F'  --or SOF
                        AND     T1.SHOP_ID = T_SHOP.SHOP_ID
                        AND     T_MODL.MODL_CD = T1.MODL_CD
                        AND     T_SHOP.CITY_ID = T_REGN.CITY_ID
                        AND     T_SHOP.OFFC_ID = T_LOCL.OFFC_ID
                        AND     T_CHNL.CHNL_ID(+) = T_SHOP.SUPP_CHNL_ID
                        AND     T_BI_CHNL_TP_02.CODE_DIV(+) = 'BI_CHNL_TP_02'
                        AND     T_BI_CHNL_TP_02.LANG_CD(+) = 'CN'
                        AND     T_BI_CHNL_TP_02.CODE_CD(+) = T_CHNL.BI_CHNL_TP_02
                       
                        AND     T_BI_CHNL_TP_03.CODE_DIV(+) = 'BI_CHNL_TP_03'
                        AND     T_BI_CHNL_TP_03.LANG_CD(+) = 'CN'
                        AND     T_BI_CHNL_TP_03.CODE_CD(+) = T_CHNL.BI_CHNL_TP_03
                       
                        GROUP BY T1.BASE_YW
                                ,T_LOCL.BRNC_ID
                                ,T_LOCL.OFFC_ID
                                ,T1.SHOP_ID
                                ,T_MODL.MKT_MODL_ID
                                ,T_MODL.BASC_MODL
                                ,T_CHNL.BI_CHNL_TP_02
                                ,T_CHNL.BI_CHNL_TP_03

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/750077/viewspace-1630126/,如需轉載,請註明出處,否則將追究法律責任。

相關文章