如何在SQL中计算上一年的销售数据

时间:2019-04-11 00:33:11

标签: sql join google-bigquery case union

我正在尝试建立一个表,按周汇总销售数据。在其中,我试图使相邻的列之一显示上一年同一财政周的销售数字(由于我所在组织的财政日历,去年有53周)。我还需要比较(比较单位/比较销售)与52周前的期间,这是完全不同的会计周(将2019年的第9周与2018年的第10周进行比较)。

我尝试同时使用联合和完全外部联接,但是考虑到我的数据的使用方式,它们效率很低(因为这是每周数据,所以联合最终效率低下,因为我需要省略日期信息初始查询,然后更新表中的列以反映数据所在的星期。这显然充满了出错的机会,但也耗时105次,或者根本没有用(尝试使用完整的外部联接返回所有列的错误答案)。我也尝试过使用CTE,但这对我也不起作用。我目前正在尝试使用CASE语句,但这也返回了空值。我不太确定下一步要去哪里

#STANDARDSQL
SELECT 
 DTL.SKU_NBR                                                      AS SKU_NBR
, SLS.STR_NBR                                                     AS STR_NBR
, CONCAT(TRIM(CAST(SKU_HIER.SKU_NBR AS STRING)), ' ', '-', ' ', TRIM(SKU_HIER.SKU_DESC))                                          AS SKU
, CONCAT(TRIM(CAST(SKU_HIER.EXT_SUB_CLASS_NBR AS STRING)), ' ', '-', ' ', TRIM(SKU_HIER.SUB_CLASS_DESC))                                    AS SUB_CLASS
, CONCAT(TRIM(CAST(SKU_HIER.EXT_SUB_SC_NBR AS STRING)), ' ', '-', ' ', TRIM(SKU_HIER.SUB_SC_DESC))                                 AS SUB_SUB_CLASS
, LOCATION.MKT_NM                                           AS MARKET_NAME
, LOCATION.RGN_NM                                           AS REGION_NAME
, LOCATION.DIV_NM                                           AS DIVISION_NAME
, LOCATION.DIV_NBR                                          AS DIVISION_NUMBER
, LOCATION.RGN_NBR                                          AS REGION_NUMBER
, LOCATION.MKT_NBR                                          AS MARKET_NUMBER
, COMP.STR_COMP_IND                                         AS COMP_IND
, COMP.PY_STR_COMP_IND                                      AS PRIOR_COMP_IND
, CALENDAR.FSCL_WK_DESC                                     AS FISCAL_WEEK
, CALENDAR.FSCL_PRD_DESC                                    AS FISCAL_PERIOD
, CALENDAR.FSCL_WK_END_DT                                   AS END_DATE
, CALENDAR.FSCL_WK_BGN_DT                                   AS BEGIN_DATE
, CALENDAR.FSCL_YR                                          AS FISCAL_YEAR_NBR
, CALENDAR.FSCL_WK_NBR                                      AS WEEK_NUMBER
, CALENDAR.FSCL_YR_WK_KEY_VAL                               AS FISCAL_KEY
, CALENDAR.LY_FYR_WK_KEY_VAL                                AS LY_FISCAL_KEY
, SUM(COALESCE(DTL.UNT_SLS,0))                              AS UNITS
, SUM(COALESCE(DTL.EXT_RETL_AMT,0) + COALESCE(DTL.TOT_GDISC_DTL_AMT,0))                                                                                        
                                                            AS SALES
, SUM(CASE
            WHEN 1=1 THEN (COALESCE(DTL.EXT_RETL_AMT,0) + COALESCE(DTL.TOT_GDISC_DTL_AMT,0)) * COMP.STR_COMP_IND
            ELSE 0 END)                                     AS COMP_SALES
, SUM(CASE
            WHEN 1=1 THEN (COALESCE(DTL.UNT_SLS,0)) * COMP.STR_COMP_IND
            ELSE 0 END)                                     AS COMP_UNITS
, SUM(CASE
            WHEN 1=1 AND SLS.SLS_DT = DATE_SUB(SLS.SLS_DT, INTERVAL 364 DAY) 
            THEN (COALESCE(DTL.EXT_RETL_AMT,0) + 
            COALESCE(DTL.TOT_GDISC_DTL_AMT,0)) * COMP.PY_STR_COMP_IND
            ELSE NULL END)                                                                                                                                                 
                                                            AS LY_COMP_SALES
, SUM(CASE
            WHEN 1=1 AND SLS.SLS_DT = DATE_SUB(SLS.SLS_DT, INTERVAL 364 DAY)  
            THEN (COALESCE(DTL.UNT_SLS,0)) * COMP.PY_STR_COMP_IND
            ELSE NULL END)                                                                                                                                                 
                                                            AS LY_COMP_UNITS
, SUM(CASE 
            WHEN SLS.SLS_DT = DATE_SUB(SLS.SLS_DT, INTERVAL 371 DAY) 
            THEN (COALESCE(DTL.EXT_RETL_AMT,0) + 
            COALESCE(DTL.TOT_GDISC_DTL_AMT,0)) 
            ELSE NULL END)                                                                                                               
                                                            AS LY_SALES
, SUM(CASE 
            WHEN SLS.SLS_DT = DATE_SUB(SLS.SLS_DT, INTERVAL 371 DAY) 
            THEN (COALESCE(DTL.UNT_SLS,0)) 
            ELSE NULL END)                                                                                                               
                                                            AS LY_UNITS


FROM `pr-edw-views.SLS.POS_SLS_TRANS_DTL`                   AS SLS

INNER JOIN 
  UNNEST (SLS.DTL)                                          AS DTL 


JOIN `pr-edw-views.SHARED.MVNDR_HIER`                       AS MVNDR  
  ON DTL.DERIV_MVNDR.MVNDR_NBR = MVNDR.MVNDR_NBR

JOIN `pr-edw-views.SHARED.SKU_HIER_FD`                      AS SKU_HIER
  ON SKU_HIER.SKU_NBR = DTL.SKU_NBR
  AND SKU_HIER.SKU_CRT_DT = DTL.SKU_CRT_DT 

JOIN `pr-edw-views.SHARED.LOC_HIER_FD`                      AS LOCATION
  ON LOCATION.LOC_NBR = SLS.STR_NBR

JOIN `pr-edw-views.SHARED.CAL_PRD_HIER_FD`                  AS CALENDAR
  ON CALENDAR.CAL_DT = SLS_DT

JOIN `pr-edw-views.SLS.STR_COMP_DAY`                        AS COMP
  ON COMP.CAL_DT = CALENDAR.CAL_DT
  AND COMP.STR_NBR = SLS.STR_NBR


WHERE CALENDAR.FSCL_WK_END_DT BETWEEN '2018-01-29' AND '2019-04-07'
AND SLS.SLS_DT BETWEEN '2018-01-29' AND '2019-04-07'
AND POS_TRANS_TYP_CD in ('S', 'R')
AND SKU_HIER.EXT_CLASS_NBR = '025-004'
AND MVNDR.MVNDR_NBR IN (74798, 60002238, 73059, 206820, 76009, 40263, 12879, 76722, 10830, 206823, 87752, 60052261, 70401, 51415, 51414)
AND SKU_HIER.LATEST_SKU_CRT_DT_FLG = TRUE


GROUP BY
 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

我目前在LY_SALESLY_UNITSLY_COMP_SALESLY_COMP_UNITS列中得到空值,尽管我知道应该有销售这些物品的地点与上一年同期相比。我想要达到的是使那些前一年的值显示在当前年值的旁边。任何帮助将不胜感激!

谢谢!

2 个答案:

答案 0 :(得分:0)

此类条件永远无法满足:SLS.SLS_DT = DATE_SUB(SLS.SLS_DT,间隔371天)。仅仅是因为SLS_DT不等于SLS_DT-371。

您可以在CTE中预聚合表(将SLS_DT按列添加到组中),然后将CASE替换为对预聚合表的联接。瞄准这样的东西:它会变成这样的东西(注意-在这种情况下没有SUM):

CASE WHEN AGGSLS.SLS_DT = DATE_SUB(SLS.SLS_DT, INTERVAL 371 DAY) 
THEN (COALESCE(AGGSLS.SUM_EXT_RETL_AMT,0) + 
COALESCE(AGGSLS.SUM_TOT_GDISC_DTL_AMT,0)) 
ELSE NULL END

答案 1 :(得分:0)

两件事: 1)WHEN 1=1可以简单地表示为WHEN TRUE,这样在不中断AND / OR链接的情况下更容易移动语句

2)获得去年的销售额。您可以从最终查询中省略年份,并使用where子句限制输出,也可以创建一个较小的表,该表具有今年的销售额,每周的去年销售额。

根据我的拙见,去年的Weeknum销售是最好的选择,因为您可以在其他地方使用它。但这与您的工作非常相似

它看起来像:

SELECT CALENDAR.FSCL_WK_DESC as week_num,
sum(case when year = year(current_date()) then (COALESCE(DTL.UNT_SLS,0)) * COMP.STR_COMP_IND else 0 end) as this_year
sum(case when year = year(current_date())-1 then (COALESCE(DTL.UNT_SLS,0)) * COMP.STR_COMP_IND else 0 end) as last_year 

然后使用week_num

联接回原始表

希望您发现它有用

干杯!