[英]Issue when joining 3 temp tables
I have an issue joining 3 temp tables.我在加入 3 个临时表时遇到问题。
I have such tables我有这样的桌子
UDT_CKB_SNAPSHOT - list of different items and events (items on sale) some promotions etc UDT_CKB_SNAPSHOT - 不同项目和事件的列表(特价商品)一些促销活动等
FCSTPERFSTATIC - Forecast FCSTPERFSTATIC - 预测
HISTWIDE_CHAIN - Actuals HISTWIDE_CHAIN - 实际值
I am going to write the whole code but the thing that doesn't work is a join when selecting all three temp tables.我将编写整个代码,但在选择所有三个临时表时不起作用的是连接。 Everything else works just fine (type just in case if someone wants to see the whole picture).
其他一切都很好(键入以防万一有人想查看整个图片)。
WITH UDT_CKB_SNAPSHOT AS
(
SELECT [UPC] ,[MASTERCHAINNAME] ,[EVENT_TYPE],[STARTDATE],[ENDDATE] ,[EVENT_NAME]
FROM [BYIntegration].[SCPOMGR].[UDT_CKB_SNAPSHOT]
WHERE [EVENT_TYPE]='Promotion' AND
[STARTDATE] BETWEEN '2021-09-26' AND '2021-12-08'
GROUP BY [UPC] ,[MASTERCHAINNAME],[EVENT_TYPE],[STARTDATE],[ENDDATE] ,[EVENT_NAME]
),
FCSTPERFSTATIC AS
( SELECT [DMDUNIT],[STARTDATE],[LOC],SUM([TOTFCST]) AS 'Forecast'
FROM [BYIntegration].[SCPOMGR].[FCSTPERFSTATIC]
WHERE [STARTDATE] BETWEEN '2021-09-26' AND '2021-12-08'
GROUP BY [DMDUNIT], [STARTDATE], [LOC]
),
HISTWIDE_CHAIN AS
( SELECT [DMDUNIT] ,[DMDPostDate],[LOC] ,SUM([HistoryQuantity]) AS 'HistoryQuantity'
FROM [BYIntegration].[SCPOMGR].[HISTWIDE_CHAIN]
WHERE [DMDPostDate] BETWEEN '2021-09-26' AND '2021-12-08'
GROUP BY [DMDUNIT], [DMDPostDate], [LOC]
)
**SELECT U.[UPC] AS 'Item', U.[MASTERCHAINNAME] AS 'Chain', U.[STARTDATE] AS 'Start Date', U.[EVENT_TYPE] , U.[EVENT_NAME], SUM(F.Forecast) AS 'Forecast', SUM(A.HistoryQuantity) AS 'Actuals'
FROM UDT_CKB_SNAPSHOT U
LEFT OUTER JOIN FCSTPERFSTATIC F
on U.[UPC]=F.[DMDUNIT] AND U.[MASTERCHAINNAME]=F.[LOC]
LEFT OUTER JOIN HISTWIDE_CHAIN A
ON U.[UPC]=a.[DMDUNIT] AND U.[MASTERCHAINNAME]=a.[LOC]
AND a.[DMDPostDate]=u.[STARTDATE] and f.[STARTDATE]=a.[DMDPostDate]
GROUP BY U.[UPC], U.[MASTERCHAINNAME], U.[STARTDATE], U.[EVENT_TYPE] , U.[EVENT_NAME]**
So the issue is the line in bold italics.所以问题是粗斜体线。
I need to join all three tables on dates to make sure I select the same inventory during the same time period.我需要在日期上加入所有三个表,以确保在同一时间段内我 select 相同的库存。 Whenever I join those three tables I either get Forecast or Actuals right, but never both.
每当我加入这三个表时,我要么得到正确的预测或实际值,但永远不会同时得到两者。
When i type this I get Forecast correct, but Actuals incorrect a.[DMDPostDate]=u.[STARTDATE] and f.[STARTDATE]=a.[DMDPostDate] (Forecast correct 6998.649, Actuals are not correct 826)当我输入这个时,我得到预测正确,但实际不正确 a.[DMDPostDate]=u.[STARTDATE] 和 f.[STARTDATE]=a.[DMDPostDate](预测正确 6998.649,实际不正确 826)
-- AND u.[STARTDATE]=f.[STARTDATE] and f.[STARTDATE]=u.[STARTDATE] (Actuals are correct 10369, Forecast not correct 8322.315) -- AND u.[STARTDATE]=f.[STARTDATE] and f.[STARTDATE]=u.[STARTDATE](实际值正确 10369,预测不正确 8322.315)
-- and a.[DMDPostDate]=f.[STARTDATE] (Forecast correct 6998.649, Actuals not correct) -- 和 a.[DMDPostDate]=f.[STARTDATE](预测正确 6998.649,实际不正确)
-- AND u.[STARTDATE]=a.[DMDPostDate] (Forecast correct 6998.649, Actuals not correct) -- AND u.[STARTDATE]=a.[DMDPostDate](预测正确 6998.649,实际不正确)
-- AND u.[STARTDATE]=f.[STARTDATE] (Actuals are correct 10369, Forecast not correct) -- AND u.[STARTDATE]=f.[STARTDATE](实际值正确 10369,预测不正确)
-- and u.[STARTDATE]=f.[STARTDATE] and u.[STARTDATE] = a.[DMDPostDate] (Forecast correct 6998.649, Actuals not correct) -- and u.[STARTDATE]=f.[STARTDATE] and u.[STARTDATE] = a.[DMDPostDate](预测正确 6998.649,实际不正确)
When I run all three queries separately everything works but when I am selecting them one of them depending how I join STARTDATE goes wrong.当我分别运行所有三个查询时,一切正常,但是当我选择其中一个时,取决于我加入 STARTDATE 的方式出错。 I've spent almost the whole day trying to troubleshoot, but i still don't understand how I can make it work.
我几乎一整天都在尝试排除故障,但我仍然不明白如何使它工作。
I will appreciate any advice.我将不胜感激任何建议。
When i type this I get Forecast correct, but Actuals incorrect a.[DMDPostDate]=u.[STARTDATE] and f.[STARTDATE]=a.[DMDPostDate] (Forecast correct 6998.649, Actuals are not correct 826)当我输入这个时,我得到预测正确,但实际不正确 a.[DMDPostDate]=u.[STARTDATE] 和 f.[STARTDATE]=a.[DMDPostDate](预测正确 6998.649,实际不正确 826)
-- AND u.[STARTDATE]=f.[STARTDATE] and f.[STARTDATE]=u.[STARTDATE] (Actuals are correct 10369, Forecast not correct 8322.315) -- AND u.[STARTDATE]=f.[STARTDATE] and f.[STARTDATE]=u.[STARTDATE](实际值正确 10369,预测不正确 8322.315)
-- and a.[DMDPostDate]=f.[STARTDATE] (Forecast correct 6998.649, Actuals not correct) -- 和 a.[DMDPostDate]=f.[STARTDATE](预测正确 6998.649,实际不正确)
-- AND u.[STARTDATE]=a.[DMDPostDate] (Forecast correct 6998.649, Actuals not correct) -- AND u.[STARTDATE]=a.[DMDPostDate](预测正确 6998.649,实际不正确)
-- AND u.[STARTDATE]=f.[STARTDATE] (Actuals are correct 10369, Forecast not correct) -- AND u.[STARTDATE]=f.[STARTDATE](实际值正确 10369,预测不正确)
-- and u.[STARTDATE]=f.[STARTDATE] and u.[STARTDATE] = a.[DMDPostDate] (Forecast correct 6998.649, Actuals not correct) -- and u.[STARTDATE]=f.[STARTDATE] and u.[STARTDATE] = a.[DMDPostDate](预测正确 6998.649,实际不正确)
From your post - it seems fairly clear that joining STARTDATEs gets you the correct actuals, and joining DMDPostDate to either start date gets the right forecast.从您的帖子中 - 加入 STARTDATEs 可以让您获得正确的实际数据,而加入 DMDPostDate 到任一开始日期似乎很明显可以得到正确的预测。
Something thing to consider - u.[STARTDATE]=f.[STARTDATE] - this clause should have no impact to the A table join, what if you added this to the F table ON clause, and then u.[STARTDATE] = a.[DMDPostDate] to the A table clause需要考虑的事情 - u.[STARTDATE]=f.[STARTDATE] - 这个子句应该对 A 表连接没有影响,如果你将它添加到 F 表 ON 子句中,然后 u.[STARTDATE] = a .[DMDPostDate] 到 A 表子句
The way it's working now, you're left joining everything from table F to everything it can join to in table A based on the U.[UPC]=F.[DMDUNIT] AND U.[MASTERCHAINNAME]=F.[LOC], without any dates.它现在的工作方式是根据 U.[UPC]=F.[DMDUNIT] AND U.[MASTERCHAINNAME]=F.[LOC] 加入从表 F 到表 A 中可以加入的所有内容, 没有任何日期。 It could be a challenge with the way it's evaluating the clauses as a result.
因此,它评估条款的方式可能是一个挑战。
Dark horse answer - you don't need to join on any dates - actuals are correct when you aren't joining dates for the A table - when you use AND u.[STARTDATE]=f.[STARTDATE] it may be applying to the U/F join which could lower the forecast number.黑马答案-您不需要在任何日期加入-当您不加入 A 表的日期时,实际值是正确的-当您使用 AND u.[STARTDATE]=f.[STARTDATE] 它可能适用于U/F 连接可能会降低预测数。
You could also troubleshoot by joining the F and A tables separately to the U table to see if you're getting the expected values then.您还可以通过将 F 和 A 表分别连接到 U 表来进行故障排除,以查看您是否获得了预期值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.