Home > Software engineering >  FIND TOP n SUMS OF A COLUMN, GROUPED BY 2 OTHER COLUMNS
FIND TOP n SUMS OF A COLUMN, GROUPED BY 2 OTHER COLUMNS

Time:10-05

enter image description here

I have a table in SQL like this. Now I want to find sum of score grouped by column ID & Name, and show just 2 highest sums for each ID as below, so how can I solve this? Thanks in advance.

enter image description here

CodePudding user response:

Can you try something like this:

Select ID, Name, Score From (
Select ID, Name, SUM(Score) score, row_number() over (partition by ID,Name order by SUM(Score) desc) rn 
from Table
group by ID,Name) allScores
where rn > 2
  • Related