MSSQL选择sum大于0的位置

时间:2016-05-05 11:48:55

标签: sql-server

我需要返回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_] 

如何达到要求的结果?

1 个答案:

答案 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_];