我正在尝试编写一个查询,该查询将允许我计算每周的销售和库存。这些表具有两个不同的粒度级别,每天计算销售额,而每周计算库存。我将销售部分汇总到每周水平,然后尝试包括所有库存(以包括在该特定星期未销售的商品)。我不太确定自己做错了什么,但是尽管使用了左联接,但只有在特定一周内售出的商品才返回值。
我分别运行了外部查询和子查询。两者都返回我期望的单个结果集。当我合并它们时,我遇到了麻烦。子查询对我来说一直有点棘手,在过去的几天里,我一直在与之战斗。我试图将子查询放在FROM子句中或作为JOIN。我还尝试过单独运行它们并加入单个表。最后,我尝试将销售部分作为内部查询,将库存部分作为外部查询,但是所有变化都没有产生预期的结果。
COALESCE(INV.SKU_NBR,SLS.SKU_NBR) AS SKU_NBR
, COALESCE(INV.STR_NBR,SLS.STR_NBR) AS STORE_NUMBER
, 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.SKU_NBR AS STRING)), ' ', '-', ' ', TRIM(SKU_HIER.SKU_DESC)) AS SKU
, CONCAT(TRIM(CAST(LOCATION.MKT_NBR AS STRING)), ' ', '-', ' ', TRIM(LOCATION.MKT_NM)) AS MARKET
, CONCAT(TRIM(CAST(LOCATION.RGN_NBR AS STRING)), ' ', '-', ' ', TRIM(LOCATION.RGN_NM)) AS REGION
, CONCAT(TRIM(CAST(LOCATION.DIV_NBR AS STRING)), ' ', '-', ' ', TRIM(LOCATION.DIV_NM)) AS DIVISION
, CONCAT(TRIM(CAST(LOCATION.RELO_TO_LOC_NBR AS STRING)), ' ', '-', ' ', TRIM(LOCATION.RELO_TO_LOC_NM)) AS STORE
, INV.BUSINESS_TYPE AS BUSINESS_TYPE
, INV.SUB_TYPE AS SUB_TYPE
, INV.BRAND AS BRAND
, CALENDAR.FSCL_WK_BGN_DT AS BEGIN_DATE
, CALENDAR.FSCL_WK_END_DT AS END_DATE
, SLS.FSCL_WK_DESC AS FISCAL_WEEK_DESCRIPTION
, CALENDAR.FSCL_WK_NBR AS WEEK_NUMBER
, INV.OH_DOLLARS AS OH_DOLLARS
, INV.OH_QTY AS OH_QTY
, INV.OO_QTY AS OO_QTY
, SUM(COALESCE(SLS.UNT_SLS,0)) AS UNIT
, SUM(COALESCE(SLS.SLS_AMT,0)) AS SALES
, SUM(COALESCE(SLS.COMP_UNT_SLS,0)) AS COMP_UNITS
, SUM(COALESCE(SLS.COMP_SLS_AMT,0)) AS COMP_SALES
, SUM(COALESCE(SLS.LY_SLS_AMT,0)) AS LY_SALES
, SUM(COALESCE(SLS.LY_UNT_SLS,0)) AS LY_UNITS
, SUM(COALESCE(SLS.LY_COMP_UNT_SLS,0)) AS LY_COMP_UNITS
, SUM(COALESCE(SLS.LY_COMP_SLS_AMT,0)) AS LY_COMP_SALES
FROM (SELECT
INV.SKU_NBR
, INV.SKU_CRT_DT
, INV.STR_NBR
, INV.WEEK_END
, SUM(COALESCE(INV.OH_QTY,0)) AS OH_QTY
, SUM(COALESCE(INV.OO_QTY,0)) AS OO_QTY
, SUM(COALESCE(INV.OH_QTY,0)*(COALESCE(INV. CURR_RETL_AMT,0))) AS OH_DOLLARS
FROM `pr-edw-views.INV.SKU_WKLY` AS INV
JOIN `np-al.data.DR` AS DR
ON INV.SKU_NBR = DR.SKU_NBR
AND INV.SKU_CRT_DT = DR.SKU_CRT_DT
AND INV.STR_NBR = DR.STR_NBR
WHERE INV.WEEK_END BETWEEN '2019-07-09' AND
DATE_SUB(CURRENT_DATE("America/New_York"), INTERVAL 7 DAY)
AND INV.WEEK_END >= DR.EXECUTION_DATE
GROUP BY 1, 2, 3, 4, 5, 6, 7, 8) AS INV
LEFT JOIN `pr-edw-views.SLS.SKU_SLS` AS SLS
ON INV.SKU_NBR = SLS.SKU_NBR
AND INV.SKU_CRT_DT = SLS.SKU_CRT_DT
AND INV.STR_NBR = SLS.STR_NBR
JOIN `pr-edw-views.SHARED.CAL_PRD_HIER_FD` AS CALENDAR
ON SLS.CAL_DT = CALENDAR.SLS_DT
JOIN `pr-edw-views.SHARED.SKU_HIER_FD` AS SKU_HIER
ON SLS.SKU_NBR = SKU_HIER.SKU_NBR
AND SLS.SKU_CRT_DT = SKU_HIER.SKU_CRT_DT
JOIN `pr-edw-views.SHARED.LOC_HIER_FD` AS LOCATION
ON SLS.STR_NBR = LOCATION.LOC_NBR =
WHERE SLS.SALE_DATE BETWEEN '2019-07-09' AND DATE_SUB(CURRENT_DATE("America/New_York"), INTERVAL 7 DAY)
AND SLS.SALE_DATE >= DR.EXECUTION_DATE
GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18```
Ultimately the end result I'm looking for is to retrieve all the sales for every item sold in a given week, as well as the inventory for those items and any other items that have not sold. Right now the query is only returning the correct sales values, but incorrect inventory values for all items, whether they were sold or not. I appreciate any guidance I can get! Thanks!