把inventorycheckpoints替换为tb1_Inventory,stockinventoryies替换为tb2_input,stockouts替换为tb5_Operatelog
时间: 2024-10-15 10:18:25 浏览: 29
好的,如果我们把原来的表名改一下,那么基于新的表名`tb1_Inventory`(盘点),`tb2_Input`(入库)和`tb5_Operatelog`(出库),重写的SQL查询会是这样的:
```sql
SELECT T1.MatName AS '物料名称',
(T1.Quantity + T2.Quantity - T3.Quantity) AS '实时库存'
FROM tb1_Inventory T1
JOIN (
SELECT MatName, SUM(Quantity) AS Quantity
FROM tb2_Input
WHERE InputDate = (SELECT MAX(InputDate) FROM tb2_Input)
GROUP BY MatName
) T2 ON T1.MatName = T2.MatName
JOIN (
SELECT MatName, SUM(Quantity) AS Quantity
FROM tb5_Operatelog
WHERE LogDate = (SELECT MIN(LogDate) FROM tb5_Operatelog)
GROUP BY MatName
) T3 ON T1.MatName = T3.MatName
WHERE T1.InventoryDate = (SELECT MAX(InventoryDate) FROM tb1_Inventory);
```
这里依然通过最新的盘点日期`InventoryDate`来获取盘点数量,并结合入库日期`InputDate`和出库日期`LogDate`的数据更新库存。
阅读全文