-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path6.sql
More file actions
30 lines (29 loc) · 837 Bytes
/
6.sql
File metadata and controls
30 lines (29 loc) · 837 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
-- Query 6: Yearly airline performance statistics
-- Calculates total flights, average departure delay, and average arrival delay
-- for each airline by year, demonstrating fact/dimension joins
WITH airline_yearly_stats AS (
SELECT
a.airline,
f.year,
COUNT(*) AS total_flights,
ROUND(AVG(f.dep_delay), 2) AS avg_departure_delay,
ROUND(AVG(f.arr_delay), 2) AS avg_arrival_delay
FROM
flights f
JOIN
airlines a ON f.carrier = a.iata_code
WHERE
f.fl_date IS NOT NULL
GROUP BY
a.airline, f.year
)
SELECT
airline AS Airline,
year AS Year,
total_flights AS Total_Flights,
avg_departure_delay AS Avg_Departure_Delay,
avg_arrival_delay AS Avg_Arrival_Delay
FROM
airline_yearly_stats
ORDER BY
year, total_flights DESC;