我想基于数量值来计算数量的总和如何在使用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-服务器) –