第二次调用仓库物料时发生重复

时间:2018-06-27 10:14:10

标签: sql sql-server

我的查询中出现一些奇怪的重复,问题是我需要能够从仓库物料表中查询两个库存数量,并且如您所见,我两次调用它,因此存在仓库物料的重复。会发现这是我的加入。

SELECT ki.[KitItemID]
      ,ki.[KitHeaderID]
      ,kh.StockCode as KitHeaderStockCode
      ,ki.[StockCode]
      ,ki.[StockDescription]
      ,ki.[StockItemID]
      ,ki.[Qty]
      ,ki.[IsBoard]
      ,ki.[IsSubAssembly],
      ISNULL(wip.ConfirmedQtyInStock,0) as InStockCs,
      ISNULL(SUM(wip2.ConfirmedQtyInStock), 0) as InStockWip,           
       @ppBatch as BatchID

FROM KitItem ki
LEFT JOIN KitHeader kh ON ki.KitHeaderID = kh.KitHeaderID
LEFT JOIN WarehouseItem wip2 ON ki.StockItemID = wip2.ItemID AND wip2.WarehouseID = (SELECT TOP 1 WIP_LocationID FROM Settings) 
INNER JOIN
dbo.WarehouseItem wip ON ki.StockItemID  =wip.ItemID INNER JOIN
dbo.Warehouse war ON wip.WarehouseID = war.WarehouseID
WHERE ki.IsBox = 0
GROUP BY 
ki.[KitItemID]
,ki.[KitHeaderID]
,kh.StockCode
,ki.[StockCode]
,ki.[StockDescription]
,ki.[StockItemID]
,ki.[Qty]
,ki.[IsBoard]
,ki.[IsSubAssembly]
,wip.ConfirmedQtyInStock
,wip2.ConfirmedQtyInStock   

2 个答案:

答案 0 :(得分:0)

ON ki.StockItemID =wip.ItemID之后,添加

and wip.ItemID=wip2.ItemID

所以每行只包含1个项目的数据,而不是2

答案 1 :(得分:0)

如果您有完整的副本,则可以在data source=192.168.1.32;initial catalog=C:\blabla\blabla.FDB;user id=blaUser;dialect=3中使用distinct,否则在select中添加一列