Skip to content

Commit 5c36ab4

Browse files
authored
Create tournament-winners.sql
1 parent 4fab008 commit 5c36ab4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

MySQL/tournament-winners.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time: O(m + n + nlogn)
2+
# Space: P(m + n)
3+
4+
SELECT group_id,
5+
player_id
6+
FROM (SELECT p.group_id,
7+
ps.player_id,
8+
Sum(ps.score) AS score
9+
FROM players p INNER JOIN
10+
(SELECT first_player AS player_id,
11+
first_score AS score
12+
FROM matches
13+
UNION ALL
14+
SELECT second_player AS player_id,
15+
second_score AS score
16+
FROM matches) ps
17+
ON p.player_id = ps.player_id
18+
GROUP BY ps.player_id
19+
ORDER BY group_id,
20+
score DESC,
21+
player_id) top_scores
22+
GROUP BY group_id

0 commit comments

Comments
 (0)