我需要返回amount_left
大于0
的结果。我尝试过这个,但这会产生错误的结果。
SELECT DISTINCT [item no_],
(SELECT Sum(quantity)
FROM dbo.[auto kada sia$item ledger entry]
WHERE [item no_] = a.[item no_]) AS 'amount_left'
FROM dbo.[auto kada sia$item ledger entry] AS a
ORDER BY a.[item no_]
如何达到要求的结果?
答案 0 :(得分:4)
SELECT [item no_], SUM(quantity) AS 'amount_left'
FROM dbo.[auto kada sia$item ledger entry]
GROUP BY [item no_]
HAVING SUM(quantity) > 0
ORDER BY [item no_];