最近一次盘点数量,入库数量,出库数量是在3个不同的表中,请重写
时间: 2024-10-15 20:18:13 浏览: 20
既然最近一次盘点数量、入库数量以及出库数量分别分布在三个不同的表中,我们需要合并这些数据。假设这三个表分别是`InventoryCheckpoints`(盘点记录),`StockInventories`(入库记录) 和 `StockOuts`(出库记录)。我们可以使用JOIN语句来关联它们:
```sql
SELECT I.MatName AS '物料名称',
(LC.Quantity + SI.Quantity - O.Quantity) AS '实时库存'
FROM InventoryCheckpoints LC
JOIN (SELECT MatName, SUM(Quantity) AS Quantity
FROM StockInventories
WHERE InventoryDate = (SELECT MAX(InventoryDate) FROM StockInventories)
GROUP BY MatName) SI ON LC.MatName = SI.MatName
JOIN (SELECT MatName, SUM(Quantity) AS Quantity
FROM StockOuts
WHERE InventoryDate = (SELECT MIN(InventoryDate) FROM StockOuts)
GROUP BY MatName) O ON LC.MatName = O.MatName
WHERE LC.InventoryDate = (SELECT MAX(InventoryDate) FROM InventoryCheckpoints);
```
这个查询首先找到每个物料的最新盘点信息(`LC`),然后结合最新入库记录(`SI`)和最早出库记录(`O`)计算库存量。
阅读全文