我有以下sql查询:
select Judge, ResultIndex, count(*) as CasesForJudge
from SRSIndexes
group by Judge, ResultIndex
列"Judge"
和"ResultIndex"
是nvarchar
类型。我收到这样的输出:
Adelina Andreeva 2a 24
Adelina Andreeva 5b 33
....
Georgy Ivanov 3b 44
Georgy Ivanov 5a 5
我想为每位评委找到总和(来自"CasesForJudge"
列)(例如:Adelina Andreeva -> 57
,Georgy Ivanov -> 49
)。我该如何修改我的查询?
答案 0 :(得分:2)
您只需要GROUP BY
Judge
列
SELECT Judge, count(*) AS CasesForJudge
FROM SRSIndexes
GROUP BY Judge
答案 1 :(得分:1)
如果你想在一个查询中两个分组,你可以尝试grouping sets
:
select Judge,
ResultIndex,
count(*) as CasesForJudge
from SRSIndexes
group by grouping sets ((Judge, ResultIndex), -- Initial grouping
(Judge)) -- Added one