ppetyr
ppetyr

Reputation: 111

I need help for a specific sql query

I have the following sql query:

select Judge, ResultIndex, count(*) as CasesForJudge
from SRSIndexes
group by Judge, ResultIndex

The columns "Judge" and "ResultIndex" are nvarchar type. I recieve output like this:

Adelina Andreeva  2a           24
Adelina Andreeva  5b           33
....
Georgy Ivanov     3b           44
Georgy Ivanov     5a           5

I want to find the sums (from "CasesForJudge" column) for each judge (for example: Adelina Andreeva -> 57, Georgy Ivanov -> 49). How should i modify my query?

Upvotes: 3

Views: 55

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

In case you want both groupings in one query you can try grouping sets:

    select Judge, 
           ResultIndex, 
           count(*) as CasesForJudge
      from SRSIndexes
  group by grouping sets ((Judge, ResultIndex), -- Initial grouping 
                          (Judge))              -- Added one 

Upvotes: 1

Fabio
Fabio

Reputation: 23490

You just need to GROUP BY your Judge column

SELECT Judge, count(*) AS CasesForJudge
FROM SRSIndexes
GROUP BY Judge

Upvotes: 2

Related Questions