Use a UNION ALL to combine scores of players whether they are in the first_player or second_player position.
Aggregate the total scores for each player using GROUP BY.
Join the aggregated scores with the Players table to get the group_id for each player.
Use the RANK() window function to assign a rank to each player within their group, ordered by total score descending and player_id ascending.
Filter the results to only include players with a rank of 1, which are the winners in their groups.
erDiagram
Players {
int player_id
int group_id
}
Matches {
int match_id
int first_player
int second_player
int first_score
int second_score
}
Players ||--o{ Matches : "contains"
Aggregation with Conditional Sum and Minimum Player ID Selection