请不要写伪代码,要写能用的SQL语句,where后面不要用between and,直接用入库日期>最近一次盘点日期,出库日期 >最近一次一次盘点日期 即可
时间: 2024-10-15 14:17:20 浏览: 11
明白了,如果你不想使用`BETWEEN`关键字,你可以直接比较日期条件。假设你的历史盘点表名为`InventoryCheck`,入库表为`Inventories`,出库表为`Outlays`,且这些表都有对应的日期字段,如`CheckDate`、`PurchaseDate`和`SaleDate`,那么SQL语句如下:
```sql
WITH LatestCheck AS (
SELECT MAX(CheckDate) AS LatestCheckDate
FROM InventoryCheck
)
SELECT
I.CheckDate,
I.PurchaseAmount,
O.SaleAmount,
(SELECT CheckQuantity FROM InventoryCheck WHERE CheckDate = L.LatestCheckDate) AS RecentCheckQuantity,
(SELECT SUM(PurchaseAmount) FROM Inventories WHERE PurchaseDate > L.LatestCheckDate) AS RecentInflows,
(SELECT SUM(SaleAmount) FROM Outlays WHERE SaleDate > L.LatestCheckDate) AS RecentOutflows,
(RecentCheckQuantity + RecentInflows) - RecentOutflows AS CurrentStock
FROM
InventoryCheck I
JOIN
LatestCheck L ON I.CheckDate = L.LatestCheckDate
JOIN
Inventories I2 ON I2.PurchaseDate > L.LatestCheckDate
JOIN
Outlays O ON O.SaleDate > L.LatestCheckDate
```
这个查询首先找出历史盘点表中最新的盘点日期,然后根据这个日期查询入库和出库的数量,最后计算当前库存。记得将`CheckDate`, `PurchaseAmount`, `SaleAmount`, `CheckQuantity`, `PurchaseAmount`, 和 `SaleAmount`替换为实际的字段名。
阅读全文