Utilisez row_number()
:
select group_id, player_id
from (
select
p.*,
row_number() over(
partition by p.group_id
order by case
when m.first_player = p.player_id then m.first_score
else m.second_score
end desc,
player_id
) rn
from players p
inner join matches m
on m.first_player = p.player_id or m.second_player = p.player_id
) x
where rn = 1
| group_id | player_id |
| -------- | --------- |
| 1 | 65 |
| 2 | 20 |
Remarque :il n'y a qu'un seul joueur dans le groupe 3 (player_id 40) et il n'a participé à aucun jeu.