-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path7.sql
More file actions
29 lines (28 loc) · 791 Bytes
/
7.sql
File metadata and controls
29 lines (28 loc) · 791 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
-- Query 7: Airline distance and air time statistics by year
-- Calculates total distance traveled and average air time for each airline
-- by year, demonstrating fact/dimension joins
WITH airline_distance_stats AS (
SELECT
a.airline,
f.year,
SUM(f.distance) AS total_distance_traveled,
ROUND(AVG(f.air_time), 2) AS avg_air_time
FROM
flights f
JOIN
airlines a ON f.carrier = a.iata_code
WHERE
f.distance IS NOT NULL
AND f.air_time IS NOT NULL
GROUP BY
a.airline, f.year
)
SELECT
airline AS Airline,
year AS Year,
total_distance_traveled AS Total_Distance_Traveled,
avg_air_time AS Avg_Air_Time
FROM
airline_distance_stats
ORDER BY
year, total_distance_traveled DESC;