-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday-06.sql
More file actions
34 lines (31 loc) · 1.01 KB
/
day-06.sql
File metadata and controls
34 lines (31 loc) · 1.01 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
-- SQL Advent Calendar - Day 6
-- Title: Ski Resort Snowfall Rankings
-- Difficulty: hard
--
-- Question:
-- Buddy is planning a winter getaway and wants to rank ski resorts by annual snowfall. Can you help him bucket these ski resorts into quartiles?
--
-- Buddy is planning a winter getaway and wants to rank ski resorts by annual snowfall. Can you help him bucket these ski resorts into quartiles?
--
-- Table Schema:
-- Table: resort_monthly_snowfall
-- resort_id: INT
-- resort_name: VARCHAR
-- snow_month: INT
-- snowfall_inches: DECIMAL
--
-- My Solution:
WITH annual_snow AS (
-- Step 1: Calculate the total annual snowfall for each resort
SELECT resort_name,
SUM(snowfall_inches) AS total_snowfall
FROM resort_monthly_snowfall
GROUP BY resort_name
)
-- Step 2: Use NTILE to bucket the resorts into 4 quartiles
SELECT
resort_name,
total_snowfall,
NTILE(4) OVER (ORDER BY total_snowfall DESC) AS snowfall_quartile
FROM annual_snow
ORDER BY snowfall_quartile, total_snowfall DESC;