我有一张桌子有库存,库存和(库存 - 库存)。我希望根据(库存 - 库存)产生的程序计算进出库存之间的差异。我是
Stock In |Stock Out|(Stock In - Stock Out)|
500 | 50 | 450 |
450 | 100 | 350 |
我遇到的问题是Stock Out是从500的原始库存中扣除。
结果,我有这个:
Stock In |Stock Out|(Stock In - Stock Out)|
500 | 50 | 450 |
0 | 100 | 400 |
0 | 150 | 350 |
我希望从差异的结果中扣除它。
这是我的问题:
SELECT Product_Code, SUM(Stock In - Stock Out) AS Stock_Balance
FROM Daily_Stock_Balance
GROUP BY Product_Code;
这是我从上述查询得到的结果:
Stock In |Stock Out|Stock Balance|
500 | 50 | 450 |
| 100 | 100 |
| 150 | 100 |
下面是我的真实数据库表
任何帮助都将受到高度赞赏。
答案 0 :(得分:2)
如何总结相关列:
SELECT Testx.dispatchdate,
Testx.stockin,
Testx.stockout,
(SELECT SUM(stockin)
FROM Testx a
WHERE a.dispatchdate <= Testx.dispatchdate) AS SumIn,
(SELECT SUM(stockout)
FROM Testx a
WHERE a.dispatchdate <= Testx.dispatchdate) AS SumOut,
[sumin] - [sumout] AS Balance
FROM Testx
ORDER BY Testx.dispatchdate;