我有如下所示的数据
Name Price
Steve 10
Arnold 15
Arnold 20
Joseph 30
Steve 25
我的要求是输出如下图所示的输出。
Sum(Arnold) sum(steve + joseph)
35 65
请为此建议一些代码。
答案 0 :(得分:2)
您将在SUM()
中使用CASE语句SELECT
SUM(CASE WHEN [Name]='Arnold' THEN [Price] ELSE 0 END) as [Sum(Arnold)],
SUM(CASE WHEN [Name] in ('Steve','Joseph') THEN [Price] ELSE 0 END) as [Sum(Steve + Joseph)]
FROM yourtable