我想基於數量值來計算數量的總和如何在使用SQL Server的情況下進行分組?
對於實例
ItemNo Quantity
------------------
111 5
111 -2
111 3
112 10
我想ItemNo
做分組和計算像下面
ItemNo Quantity Positive Negative
-----------------------------------------
111 6 8 -2
112 10 10 0
我想這樣的
SELECT
ItemNo,
Sum(Quantity),
Case when Quantity >= 0 then sum(quantity) else 0 end POSITIVE,
Case when Quantity < 0 then sum(quantity) else 0 end Negative
From
Sales
Group By
ItemNo,
Quantity
我知道這個分組是錯誤的。我的查詢應該如何?
感謝
可能的重複[在SQL Server中使用CAS函數和SUM函數](https://stackoverflow.com/questions/17612387/use-case-statement-with-sum-function-in-sql-服務器) –