-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path1.sql
More file actions
37 lines (36 loc) · 964 Bytes
/
1.sql
File metadata and controls
37 lines (36 loc) · 964 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
32
33
34
35
36
37
-- Query 1: Airline performance statistics for 2020
-- Calculates total flights, market share percentage, cancellation percentage,
-- and diversion percentage for each airline, demonstrating fact/dimension joins
WITH flight_stats AS (
SELECT
a.airline,
COUNT(*) AS volume,
SUM(f.diverted) AS diverted,
SUM(f.cancelled) AS cancelled
FROM
flights f
JOIN
airlines a ON f.carrier = a.iata_code
WHERE
f.year = 2020
GROUP BY
a.airline
),
total_volume AS (
SELECT
SUM(volume) AS total_volume
FROM
flight_stats
)
SELECT
fs.airline,
fs.volume AS flight_count,
ROUND(100 * fs.volume / tv.total_volume, 2) AS market_share_pct,
ROUND(100 * (fs.cancelled / fs.volume), 2) AS cancelled_pct,
ROUND(100 * (fs.diverted / fs.volume), 2) AS diverted_pct
FROM
flight_stats fs
CROSS JOIN
total_volume tv
ORDER BY
flight_count DESC;