我需要特定sql查询的帮助

时间:2016-12-06 06:38:00

标签: sql

我有以下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 -> 57Georgy Ivanov -> 49)。我该如何修改我的查询?

2 个答案:

答案 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