Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Hard/01194-tournament-winners.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,38 @@ from cte
where rnk = 1

-- wayfair- 1


#####################################################################################


WITH
s AS (
SELECT first_player AS player_id, first_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.first_player = p.player_id
UNION ALL
SELECT second_player AS player_id, second_score AS score, group_id
FROM
Matches AS m
JOIN Players AS p ON m.second_player = p.player_id
),
t AS (
SELECT group_id, player_id, SUM(score) AS scores
FROM s
GROUP BY player_id
),
p AS (
SELECT
group_id,
player_id,
RANK() OVER (
PARTITION BY group_id
ORDER BY scores DESC, player_id
) AS rk
FROM t
)
SELECT group_id, player_id
FROM p
WHERE rk = 1;