2016-05-05 58 views
-2

我需要返回結果,其中amount_left大於0。我試過這個,但是這給出了錯誤的結果。MSSQL select where sum sum than 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

顯示一些示例數據和預期的結果 – Mihai

+0

謝謝你,但我認爲這是沒有必要的,因爲肖恩給了我正是我需要 – HellOfACode

+1

如果是的話,我建議你紀念他的答案接受。 – gofr1

回答

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