-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExpert-SQL48.sql
More file actions
28 lines (26 loc) · 1023 Bytes
/
DataExpert-SQL48.sql
File metadata and controls
28 lines (26 loc) · 1023 Bytes
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
-- Question: Who are the top 10 NBA players by consecutive 20+ point seasons?
-- https://www.dataexpert.io/question/consecutive-20-point-seasons
WITH qualifying_seasons AS (
SELECT player_name,season
FROM bootcamp.nba_player_seasons
WHERE pts > 20),
season_groups AS (
SELECT player_name,season,season - ROW_NUMBER() OVER (PARTITION BY player_name ORDER BY season) AS grp
FROM qualifying_seasons),
streaks AS (
SELECT player_name, grp, COUNT(*) AS consecutive_seasons
FROM season_groups
GROUP BY player_name, grp),
player_best_streaks AS (
SELECT player_name, MAX(consecutive_seasons) AS max_consecutive_seasons
FROM streaks
GROUP BY player_name),
ranked_players AS (
SELECT player_name,max_consecutive_seasons,
dense_rank() OVER (ORDER BY max_consecutive_seasons DESC) AS rnk
FROM player_best_streaks)
SELECT player_name, max_consecutive_seasons AS consecutive_seasons
FROM ranked_players
WHERE rnk <= 10
ORDER BY consecutive_seasons DESC,
player_name ASC;