-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathFlight-Analysis.sql
74 lines (67 loc) · 1.9 KB
/
Flight-Analysis.sql
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- Get specific flight given by its ID
select f.id, f.creation_time, f.title
from flight f
where f.id = 42
-- Get latest flight on a given date
select f.id, f.creation_time, f.title
from flight f
where strftime('%Y-%m-%d', f.creation_time) = '2021-05-26'
order by f.creation_time desc
limit 1
-- Get most recent flight of the logbook
select f.id, f.creation_time, f.title
from flight f
order by f.creation_time desc
limit 1
-- Select specific values from tables position, aircraft and flight
select f.id as flight_id,
f.start_zulu_sim_time,
f.end_zulu_sim_time,
f.flight_number,
a.type,
p.timestamp,
p.latitude,
p.longitude,
p.altitude,
p.pitch,
p.bank,
p.true_heading,
p.velocity_z * 0.5924838012959 as speed_in_knots
from position p
join aircraft a
on p.aircraft_id = a.id
join flight f
on a.flight_id = f.id
and a.seq_nr = 1
-- Select the above data from the most recent flight in the logbook
where f.id = (select ff.id
from flight ff
order by creation_time desc
limit 1)
-- Select the top 10 aircraft types (number of flights)
select a.type as AircraftType,
count(a.type) as Count
from aircraft a
group by a.type
order by count(a.type) desc
limit 10
-- Select the top 10 flights (flight duration in minutes)
select f.id, f.title, a.type, max(p.timestamp) / 1000 / 60 as minutes
from aircraft a
join position p
on p.aircraft_id = a.id
join flight f
on a.flight_id = f.id
group by p.aircraft_id
order by minutes desc
limit 10
-- Select the top 10 aircraft types (total flight time)
select a.type, sum(a.minutes) as total_minutes
from (select aa.type, max(pp.timestamp) / 1000 / 60 as minutes
from aircraft aa
join position pp
on pp.aircraft_id = aa.id
group by pp.aircraft_id) a
group by a.type
order by total_minutes desc
limit 10