[英]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.