Home > OS >  Select a row based on where have maximum by one column
Select a row based on where have maximum by one column

Time:07-09

I have a table called match_score which have following data

id participant round score
1 gabe 1 100
2 john 1 90
3 duff 1 80
4 vlad 1 85
5 gabe 2 75
6 john 2 70

Let's just say that round 1 is the preliminary round and 2 is the final round

I want to rank the result based on the score and grouped by the participant , if I'm using some normal sql group by participant and order by score desc.

vlad are the 1st, duff 2nd and gabe 3rd, which one is wrong.

what i want is

1st gabe with 75 point in the final round

2nd john with 70 point in the final round

3rd vlad with 85 point in the preliminary round

4th duff with 80 point in the preliminary round

CodePudding user response:

maybe something like this:

select  h.participant, h.round, h.score 
from    match_score h
where not exists (select 1 from match_score t where t.participant = h.participantand t.round > h.round)
order by h.round desc, h.score desc;
  •  Tags:  
  • sql
  • Related