-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataExpert-SQL18.sql
More file actions
31 lines (27 loc) · 985 Bytes
/
DataExpert-SQL18.sql
File metadata and controls
31 lines (27 loc) · 985 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
29
30
31
-- Question: Comparing State Fatal Collisions to the National Average
-- https://www.dataexpert.io/question/state-fatal-collisions
-- Approach 1:
WITH national_average AS (
SELECT *, AVG(fatal_collisions_per_billion_miles) OVER() AS national_avg
FROM playground.bad_drivers
)
SELECT state, fatal_collisions_per_billion_miles,
CASE
WHEN fatal_collisions_per_billion_miles > national_avg THEN 'Above Average'
ELSE 'Below Average'
END AS comparison_to_national_avg
FROM national_average
ORDER BY state;
-- Approach 2:
WITH national_average AS (
SELECT AVG(fatal_collisions_per_billion_miles) AS national_avg
FROM playground.bad_drivers
)
SELECT b.state, b.fatal_collisions_per_billion_miles,
CASE
WHEN b.fatal_collisions_per_billion_miles > n.national_avg THEN 'Above Average'
ELSE 'Below Average'
END AS comparison_to_national_avg
FROM national_average n
CROSS JOIN playground.bad_drivers b
ORDER BY state;