-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1194-TournamentWinners.sql
More file actions
164 lines (158 loc) · 5.69 KB
/
1194-TournamentWinners.sql
File metadata and controls
164 lines (158 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
-- 1194. Tournament Winners
-- Table: Players
-- +-------------+-------+
-- | Column Name | Type |
-- +-------------+-------+
-- | player_id | int |
-- | group_id | int |
-- +-------------+-------+
-- player_id is the primary key (column with unique values) 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 (column with unique values) 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 belong 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 a solution to find the winner in each group.
-- Return the result table in any order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- 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 |
-- +------------+--------------+---------------+-------------+--------------+
-- Output:
-- +-----------+------------+
-- | group_id | player_id |
-- +-----------+------------+
-- | 1 | 15 |
-- | 2 | 35 |
-- | 3 | 40 |
-- +-----------+------------+
-- Create table If Not Exists Players (player_id int, group_id int)
-- Create table If Not Exists Matches (match_id int, first_player int, second_player int, first_score int, second_score int)
-- Truncate table Players
-- insert into Players (player_id, group_id) values ('10', '2')
-- insert into Players (player_id, group_id) values ('15', '1')
-- insert into Players (player_id, group_id) values ('20', '3')
-- insert into Players (player_id, group_id) values ('25', '1')
-- insert into Players (player_id, group_id) values ('30', '1')
-- insert into Players (player_id, group_id) values ('35', '2')
-- insert into Players (player_id, group_id) values ('40', '3')
-- insert into Players (player_id, group_id) values ('45', '1')
-- insert into Players (player_id, group_id) values ('50', '2')
-- Truncate table Matches
-- insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('1', '15', '45', '3', '0')
-- insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('2', '30', '25', '1', '2')
-- insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('3', '30', '15', '2', '0')
-- insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('4', '40', '20', '5', '2')
-- insert into Matches (match_id, first_player, second_player, first_score, second_score) values ('5', '35', '50', '1', '1')
-- UNION ALL
SELECT
group_id,
player_id
FROM
(
SELECT
group_id,
player_id,
SUM(score) AS score
FROM
(
( -- 每个用户总的 first_score
SELECT
p.group_id,
p.player_id,
SUM(m.first_score) AS score
FROM
Players AS p
JOIN
Matches AS m
ON p.player_id = m.first_player
GROUP BY
p.player_id
)
UNION ALL
( -- 每个用户总的 second_score
SELECT
p.group_id,
p.player_id,
SUM(m.second_score) AS score
FROM
Players AS p
JOIN
Matches AS m
ON p.player_id = m.second_player
GROUP BY
p.player_id
)
) AS s
GROUP BY
player_id
ORDER BY
score DESC, player_id
) AS result
GROUP BY
group_id
-- rank
SELECT
group_id,
player_id
FROM
(
SELECT
group_id,
t2.player_id,
RANK() OVER(PARTITION BY group_id ORDER BY score DESC, t2.player_id) rk -- 如果平局,player_id 最小 的选手获胜
FROM
(
SELECT
player_id,
sum(score) AS score
FROM
(
(
SELECT first_player AS player_id, first_score AS score FROM matches
)
UNION ALL
(
SELECT second_player AS player_id, second_score AS score FROM matches
)
) t1 group by player_id
) AS t2
LEFT JOIN players AS p
ON t2.player_id = p.player_id
) AS t3
WHERE rk = 1;