![](/img/trans.png)
[英]Return SUM(column) from a table and SUM(column) from another table
[英]Sum a column from a table and subtract it to a summed column in another table
我有两个表TblInventory和TblWithdrawal。 TblInventory具有productID和TblWithdrawal。
通过产品ID分组,我怎么和的数量在TblInventory和SUM
的数量在TblWithdrawal然后SUBTRACT
其彼此,所以我可以在每个产品ID的数量剩余或差异?
这就是我的想法,但是当我运行查询时,它要求我输入一个错误的参数,因为我希望它自动遍历表并对每个ProductID进行操作。
SELECT TblInventory.ProductID,
SUM(TblInventroy.Quantity) - SUM(TblWithdrawals.Quantity) As [Remaining]
FROM TblInventory
LEFT JOIN TblWithdrawals ON TblInventory.ProductID = TblWithdrawals.ProductID
WHERE TblInventory.ProductID = TblWithdrawals.ProductID
GROUP BY TblInventory.ProductID
谢谢。 我真的需要解决这个问题!
不要使用WHERE子句。 并使用INNER JOIN代替LEFT JOIN
使用子查询,首先编写组sql,然后根据需要将其加入。 看下面的sql。
选择剩余的invent.ProductID,invent.invQty-withdrawl.withdrQty
FROM(SELECT ProductID,SUM(Quantity)as invQty
从TblInventory)作为发明
左外连接
(SELECT ProductID,SUM(Quantity)asdrQty
FROM TblWithdrawals)作为提款
开启invQty.ProductID = withdrawl.ProductID
试试这个最好的祝愿
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.