Ik heb geen PostgreSQL-achtergrond, maar laten we eens kijken of dit werkt:
Ik zou dit beginnen door het te vereenvoudigen, door een query te schrijven die eerst de totale score per speler retourneert:
SELECT player_id, SUM(score) score
FROM (
SELECT first_player as player_id, first_score as score
FROM matches
UNION ALL
SELECT second_player, second_score
FROM matches
)
GROUP BY player_id
Voeg nu die dataset toe aan spelers om de groepen te vinden:
SELECT w.player_id, p.group_id, w.score
FROM
(
SELECT player_id, SUM(score) score
FROM (
SELECT first_player as player_id, first_score as score
FROM matches
UNION ALL
SELECT second_player, second_score
FROM matches
)
GROUP BY player_id
) as w
inner join players p
on p.player_id = w.player_id
Nu hebben we alle spelers, hun totaalscore en hun groep. We willen de winnaar per groep identificeren? We kunnen gebruik maken van rangschikking functies om dit te doen:
SELECT
w.player_id,
p.group_id,
w.score,
RANK() OVER (PARTITION BY p.group_id ORDER BY score DESC) as group_placement
FROM
(
SELECT player_id, SUM(score) score
FROM (
SELECT first_player as player_id, first_score as score
FROM matches
UNION ALL
SELECT second_player, second_score
FROM matches
)
GROUP BY player_id
) as w
inner join players p
on p.player_id = w.player_id
Nu kiezen we gewoon de beste in elke groep (rang =1) met behulp van WHERE
SELECT
player_id,
group_id
FROM
(
SELECT
w.player_id,
p.group_id,
w.score,
RANK() OVER (PARTITION BY p.group_id ORDER BY score DESC) as group_placement
FROM
(
SELECT player_id, SUM(score) score
FROM (
SELECT first_player as player_id, first_score as score
FROM matches
UNION ALL
SELECT second_player, second_score
FROM matches
)
GROUP BY player_id
) as w
inner join players p
on p.player_id = w.player_id
) as gp
WHERE group_placement = 1
Ziet er ingewikkeld uit? ja, maar u kunt zien dat het uiteindelijke resultaat beetje bij beetje wordt geleverd. Elke stap hiervan is een 'subtabel' en u kunt de gegevens op elk punt uitvoeren en observeren.