-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path26.AK-Tournament Winners problem.sql
54 lines (45 loc) · 1.48 KB
/
26.AK-Tournament Winners problem.sql
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
select * from players;
select * from matches;
#write the sql querry to find winner in each group
# the winner in each group is the palyer who scored the maximum total points within the group,in the case of tie,the lowest palyer id wins
with player_score as (
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)
,final_scores as (
select p.group_id,ps.player_id,sum(score) as score
from player_score ps
inner join players p on p.player_id=ps.player_id
group by p.group_id,ps.player_id)
,final_ranking as(
select *
,rank() over(partition by group_id order by score desc, player_id asc) as rn
from final_scores)
select * from final_ranking where rn=1;
create table players
(player_id int,
group_id int);
insert into players values (15,1);
insert into players values (25,1);
insert into players values (30,1);
insert into players values (45,1);
insert into players values (10,2);
insert into players values (35,2);
insert into players values (50,2);
insert into players values (20,3);
insert into players values (40,3);
select * from players;
select * from matches;
create table matches
(
match_id int,
first_player int,
second_player int,
first_score int,
second_score int);
insert into matches values (1,15,45,3,0);
insert into matches values (2,30,25,1,2);
insert into matches values (3,30,15,2,0);
insert into matches values (4,40,20,5,2);
insert into matches values (5,35,50,1,1);
select * from matches;