Skip to content

Commit 173bed9

Browse files
authored
Create 02173-longest-winning-streak.sql
1 parent 353eb21 commit 173bed9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Hard/02173-longest-winning-streak.sql

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- first, do a rank on all roecords for user, and a rank on each result for each user
2+
-- for each result, there will be a streak(count of records)- the differnce between the two ranks will be same for each streak- we'll filter out only streaks where type = Win
3+
-- in the final result, pull the max streak for each player in Matches table. If that player is not in cte, then 0
4+
5+
with ranked as
6+
(select *,
7+
row_number() over(partition by player_id order by match_day) as all_rnk,
8+
row_number() over(partition by player_id, result order by match_day) as result_rnk
9+
from Matches),
10+
11+
win_streak_player as
12+
(select player_id, count(*) as win_streak
13+
from ranked
14+
where result = 'Win'
15+
group by 1, all_rnk - result_rnk)
16+
17+
select m.player_id, ifnull(max(win_streak), 0) as longest_streak
18+
from Matches m
19+
left join win_streak_player w
20+
using(player_id)
21+
group by 1
22+
23+
-- amazon- 1

0 commit comments

Comments
 (0)