简体   繁体   English

加入 2 个临时表并使用 GROUP BY

[英]Joining 2 temp tables and issue with GROUP BY

I have such a situation.我有这样的情况。 For some reason, when I join two temporary tables one column gets completely different results.出于某种原因,当我加入两个临时表时,一列会得到完全不同的结果。 Depending whether I group by LOC column from Actuals or Forecast, one of the column gives correct results and another one gets something totally weird.根据我是按实际值还是预测中的 LOC 列分组,其中一列给出正确的结果,另一列得到完全奇怪的结果。

The results are accurate for Actuals (1718789) but wrong for Forecast结果对于 Actuals (1718789) 是准确的,但对于 Forecast 是错误的

WITH ACTUALS AS
(
  SELECT [LOC], [DMDUNIT], [DMDPostDate], 
  SUM(HistoryQuantity) AS 'Actuals'
  FROM SCPOMGR.HISTWIDE_CHAIN
  GROUP BY [LOC], [DMDUNIT], [DMDPostDate]    
),

Forecast AS
(
  SELECT [LOC], [DMDUNIT], [STARTDATE],
  SUM(TOTFCST) AS 'Forecast'
  FROM SCPOMGR.FCSTPERFSTATIC
  -- Forecast Albertsons 99484.136 (wrong: should be 122880.591)
  GROUP BY [LOC], [DMDUNIT], [STARTDATE]   
)
 SELECT A.[LOC],  SUM(A.Actuals) AS 'Actuals', SUM(F.Forecast) AS 'Forecast'
 FROM Actuals A FULL OUTER JOIN Forecast F 

  on A.[DMDUNIT] = F.[DMDUNIT] 
  AND f.[STARTDATE] = a.[DMDPostDate] 
  and a.[LOC] = f.[LOC]
    
  GROUP BY A.[LOC]
  ORDER BY A.[LOC]   

When I change GROUP BY from A.[LOC] to F.[LOC] I have the reverse effect.当我将 GROUP BY 从 A.[LOC] 更改为 F.[LOC] 我有相反的效果。 Now my Actuals are correct, but Forecast is not.现在我的实际值是正确的,但预测不是。

Something is wrong with the GROUP BY. GROUP BY 有问题。 Depending if I GROUP BY a.[LOC] I get a correct result for 'Actuals' but incorrect for 'Forecast', if I GROUP BY f.[LOC] I get a correct result for 'Forecast', but incorrect for 'Actuals'.取决于我是否 GROUP BY a.[LOC] 我得到一个正确的结果为 'Actuals' 但不正确的 'Forecast',如果我 GROUP BY f.[LOC] 我得到一个正确的结果 'Forecast' 但不正确的 'Actuals '。

WITH ACTUALS AS
(
  SELECT [LOC], [DMDUNIT], [DMDPostDate], 
  SUM(HistoryQuantity) AS 'Actuals'
  FROM SCPOMGR.HISTWIDE_CHAIN
  -- Actuals 80324 (Albertsons) grouped by F.[LOC] - incorrect (should be 1718789)
  GROUP BY [LOC], [DMDUNIT], [DMDPostDate]    
),

Forecast AS
(
  SELECT [LOC], [DMDUNIT], [STARTDATE],
  SUM(TOTFCST) AS 'Forecast'
  FROM SCPOMGR.FCSTPERFSTATIC
  -- Forecast Albertsons 122880.591 (Albertsons) grouped by f.[LOC] - correct
  GROUP BY [LOC], [DMDUNIT], [STARTDATE]   
)
 SELECT F.[LOC],  SUM(F.Forecast) AS 'Forecast', SUM(A.Actuals) AS 'Actuals'
 FROM Forecast F  FULL OUTER JOIN   Actuals A

  on F.[DMDUNIT] = A.[DMDUNIT] 
  AND F.[STARTDATE] = A.[DMDPostDate]
  and F.[LOC] = A.[LOC]


  GROUP BY F.[LOC]
  ORDER BY F.[LOC]

It is technically the same code just GROUP BY throws off the results of one of the columns.它在技术上是相同的代码,只是 GROUP BY 抛出了其中一列的结果。

Does someone know how it can be fixed.有人知道如何修复它。 I need to keep the JOIN on those 3 fields我需要在这 3 个字段上保留 JOIN

  on A.[DMDUNIT] = F.[DMDUNIT] 
  AND f.[STARTDATE] = a.[DMDPostDate] 
  and a.[LOC] = f.[LOC]

You're using a FULL OUTER JOIN, so I assume there will be unmatched rows between the two tables and LOC will sometimes be NULL in either table.您正在使用 FULL OUTER JOIN,所以我假设两个表之间会有不匹配的行,并且 LOC 有时会在任一表中为 NULL。 Use COALESCE to get the value from either table when one is NULL.当一个表是 NULL 时,使用 COALESCE 从任一表中获取值。

WITH ACTUALS AS
(
  SELECT [LOC], [DMDUNIT], [DMDPostDate], 
  SUM(HistoryQuantity) AS 'Actuals'
  FROM SCPOMGR.HISTWIDE_CHAIN
  GROUP BY [LOC], [DMDUNIT], [DMDPostDate]    
),

Forecast AS
(
  SELECT [LOC], [DMDUNIT], [STARTDATE],
  SUM(TOTFCST) AS 'Forecast'
  FROM SCPOMGR.FCSTPERFSTATIC
  -- Forecast Albertsons 99484.136 (wrong: should be 122880.591)
  GROUP BY [LOC], [DMDUNIT], [STARTDATE]   
)
 SELECT COALESCE(a.[LOC], f.[LOC]) as LOC,  SUM(A.Actuals) AS 'Actuals', SUM(F.Forecast) AS 'Forecast'
 FROM Actuals A FULL OUTER JOIN Forecast F 

  on A.[DMDUNIT] = F.[DMDUNIT] 
  AND f.[STARTDATE] = a.[DMDPostDate] 
  and a.[LOC] = f.[LOC]
    
  GROUP BY COALESCE(a.[LOC], f.[LOC])
  ORDER BY LOC  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM