SQL Server JOIN表中的ID重复生成错误的总和(案例)

时间:2019-06-13 19:29:02

标签: sql sql-server tsql

我有以下查询,给我不正确的总和,当将Table Runlog与Table Palletitem连接在一起时,它重复了Cases的总和。我尝试了几种不同的查询,也尝试在子查询中获取总和,但是我无法摆脱重复的问题。我当前的查询尽可能的好。感谢您的帮助

预期结果

ProgramFilesDir

具有当前代码的当前结果


RunID   Cases    LotNumber
1749    416.00   B703, B705

当前代码


RunID   Cases    LotNumber
1749    520.00   B703, B705

下面的示例数据

表格

SELECT distinct RunLog.RunID,
  Sum(RunLog.casecount) AS Cases,
    stuff((SELECT DISTINCT ', ' + cast(lot.lotnumber AS varchar(20))
                           FROM         PalletItem t2 inner JOIN
                                              Lot ON Lot.ID = t2.Lotid
                                             AND t2.RunID = RunLog.RunID
                           WHERE      
                            t2.datepalletized>='2019-06-10 16:52:00'
                             and t2.datepalletized<='2019-06-10 20:00:00'
                             group by lot.lotnumber
                            FOR XML PATH('')), 1, 1, '') AS [LotNumber]

FROM RunLog
   INNER JOIN Lot on Lot.runid=RunLog.runid
   INNER JOIN PalletItem ON PalletItem.LotNumber = Lot.ID 
   AND palletitem.palletid=runlog.palletid 
   WHERE RunLog.ActionDate>='2019-06-10 16:52:00'
   AND RunLog.ActionDate<='2019-06-10 19:02:00'
GROUP BY RunLog.RunID

2 个答案:

答案 0 :(得分:1)

您的两个INNER JOIN引入了多对多连接。这就是为什么重复的原因。而且,您没有使用select中这些表中的列。您不需要distinct,该表已经有group by

SELECT RunLog.RunID,
  Sum(RunLog.casecount) AS Cases,
    stuff((SELECT DISTINCT ', ' + cast(lot.lotnumber AS varchar(20))
                           FROM         PalletItem t2 inner JOIN
                                              Lot ON Lot.ID = t2.Lotid
                                             AND t2.RunID = RunLog.RunID
                           WHERE      
                            t2.datepalletized>='2019-06-10 16:52:00'
                             and t2.datepalletized<='2019-06-10 20:00:00'
                             group by lot.lotnumber
                            FOR XML PATH('')), 1, 1, '') AS [LotNumber]

FROM RunLog
   --INNER JOIN Lot on Lot.runid=RunLog.runid
   --INNER JOIN PalletItem ON PalletItem.Lotid = Lot.ID 
   --AND palletitem.palletid=runlog.palletid 
   WHERE RunLog.ActionDate>='2019-06-10 16:52:00'
   AND RunLog.ActionDate<='2019-06-10 19:02:00'
GROUP BY RunLog.RunID

答案 1 :(得分:0)

运行:

SELECT Sum(RunLog.casecount) AS Cases, count(*) rows
FROM RunLog
--   INNER JOIN Lot on Lot.runid=RunLog.runid
--   INNER JOIN PalletItem ON PalletItem.LotNumber = Lot.ID 
--   AND palletitem.palletid=runlog.palletid 
   WHERE RunLog.ActionDate>='2019-06-10 16:52:00'
   AND RunLog.ActionDate<='2019-06-10 19:02:00'
--GROUP BY RunLog.RunID

然后

SELECT Sum(RunLog.casecount) AS Cases, count(*) rows
FROM RunLog
     INNER JOIN Lot on Lot.runid=RunLog.runid
--   INNER JOIN PalletItem ON PalletItem.LotNumber = Lot.ID 
--   AND palletitem.palletid=runlog.palletid 
   WHERE RunLog.ActionDate>='2019-06-10 16:52:00'
   AND RunLog.ActionDate<='2019-06-10 19:02:00'
--GROUP BY RunLog.RunID

然后

SELECT Sum(RunLog.casecount) AS Cases, count(*) rows
FROM RunLog
     INNER JOIN Lot on Lot.runid=RunLog.runid
     INNER JOIN PalletItem ON PalletItem.LotNumber = Lot.ID 
     AND palletitem.palletid=runlog.palletid 
   WHERE RunLog.ActionDate>='2019-06-10 16:52:00'
   AND RunLog.ActionDate<='2019-06-10 19:02:00'
--GROUP BY RunLog.RunID

Sum Casecount值之前,要查看联接如何将重复项引入结果中。