Tournament Winners Problem
#1194
Description
LeetCode Problem 1194.
Table: Players
+-------------+-------+
| Column Name | Type |
+-------------+-------+
| player_id | int |
| group_id | int |
+-------------+-------+
player_id is the primary key of this table.
Each row of this table indicates the group of each player.
Table: Matches
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| match_id | int |
| first_player | int |
| second_player | int |
| first_score | int |
| second_score | int |
+---------------+---------+
match_id is the primary key of this table.
Each row is a record of a match, first_player and second_player contain the player_id of each match.
first_score and second_score contain the number of points of the first_player and second_player respectively.
You may assume that, in each match, players belongs to the same group.
The winner in each group is the player who scored the maximum total points within the group. In the case of a tie, the lowest player_id wins.
Write an SQL query to find the winner in each group.
The query result format is in the following example:
Players table:
+-----------+------------+
| player_id | group_id |
+-----------+------------+
| 15 | 1 |
| 25 | 1 |
| 30 | 1 |
| 45 | 1 |
| 10 | 2 |
| 35 | 2 |
| 50 | 2 |
| 20 | 3 |
| 40 | 3 |
+-----------+------------+
Matches table:
+------------+--------------+---------------+-------------+--------------+
| match_id | first_player | second_player | first_score | second_score |
+------------+--------------+---------------+-------------+--------------+
| 1 | 15 | 45 | 3 | 0 |
| 2 | 30 | 25 | 1 | 2 |
| 3 | 30 | 15 | 2 | 0 |
| 4 | 40 | 20 | 5 | 2 |
| 5 | 35 | 50 | 1 | 1 |
+------------+--------------+---------------+-------------+--------------+
Result table:
+-----------+------------+
| group_id | player_id |
+-----------+------------+
| 1 | 15 |
| 2 | 35 |
| 3 | 40 |
+-----------+------------+
Solution
Approach: Unpivot the Scores, Total per Player, ROW_NUMBER
A player can appear as first_player in some matches and as second_player in others, and both count toward their total.
- Unpivot. Turn each match into two rows,
(first_player, first_score)and(second_player, second_score), withUNION ALL.UNIONwould be wrong here: it would merge two equal rows, such as the same player scoring 2 in two matches. - Total per player. Start from
PlayersandLEFT JOINthe scores, so a player who played no match is kept with a total of 0 (COALESCE). Group bygroup_idandplayer_id. - Pick the winner.
ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY total_score DESC, player_id)puts the highest total first and, on a tie, the lowestplayer_idfirst. Keep row 1 of each group.
In group 2, players 35 and 50 both score 1, so 35 wins with the lower id.
The Pandas version stacks the two (player, score) column pairs, sums the scores per player, maps the totals onto all players with 0 for missing ones, sorts by group, score (descending) and id, and keeps the first row per group.
WITH scores AS (
-- One row per player per match, whichever side they played on.
SELECT first_player AS player_id, first_score AS score
FROM Matches
UNION ALL
SELECT second_player, second_score
FROM Matches
),
totals AS (
SELECT p.group_id, p.player_id,
COALESCE(SUM(s.score), 0) AS total_score
FROM Players AS p
LEFT JOIN scores AS s
ON s.player_id = p.player_id
GROUP BY p.group_id, p.player_id
),
ranked AS (
SELECT group_id, player_id,
ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY total_score DESC, player_id) AS place
FROM totals
)
SELECT group_id, player_id
FROM ranked
WHERE place = 1;