-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2173.sql
29 lines (28 loc) · 820 Bytes
/
2173.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
-- [ LeetCode ] 2173. Longest Winning Streak
SELECT
player_id,
IFNULL(longest_streak, 0) AS longest_streak
FROM (
SELECT DISTINCT player_id
FROM Matches
) AS Players
LEFT JOIN (
SELECT
DISTINCT player_id,
MAX(COUNT(match_day)) OVER(PARTITION BY player_id) AS longest_streak
FROM (
SELECT
player_id,
match_day,
result,
(
CAST(ROW_NUMBER() OVER(PARTITION BY player_id ORDER BY match_day ASC) AS UNSIGNED)
-
CAST(COUNT(IF(result = 'Win', 1, NULL)) OVER(PARTITION BY player_id ORDER BY match_day ASC) AS UNSIGNED)
) AS diff
FROM Matches
) AS ConsecutiveWinnings
WHERE result = 'Win'
GROUP BY player_id, diff
) AS LongestStreaks
USING (player_id);