| 1 | create view AirplaneAvailability (AirplaneId, RegistrationNumber, Active, AirplaneModelId, AirplaneModel,
|
|---|
| 2 | Manufacturer, RequiredAttendants, NumberOfSeats, LastFlightId, LastFlightNumber, LastFlightOrigin,
|
|---|
| 3 | LastFlightDestination, LastFlightDeparture, LastFlightArrival, NextFlightId, NextFlightNumber,
|
|---|
| 4 | NextFlightOrigin, NextFlightDestination, NextFlightDeparture, NextFlightArrival,
|
|---|
| 5 | HoursSinceLastFlight, HoursUntilNextFlight, Availability) as
|
|---|
| 6 | select airplane.id, airplane.registrationnumber, airplane.active,
|
|---|
| 7 | airplanemodel.id, airplanemodel.model, airplanemodel.manufacturer,
|
|---|
| 8 | airplanemodel.requiredattendants, airplanemodel.numberofseats,
|
|---|
| 9 | lf.flightid, lf.flightnumber, lf.origin, lf.destination,
|
|---|
| 10 | lf.departure, lf.arrival,
|
|---|
| 11 | nf.flightid, nf.flightnumber, nf.origin, nf.destination,
|
|---|
| 12 | nf.departure, nf.arrival,
|
|---|
| 13 | round(extract(epoch from (now() - lf.arrival)) / 3600, 1),
|
|---|
| 14 | round(extract(epoch from (nf.departure - now())) / 3600, 1),
|
|---|
| 15 | case
|
|---|
| 16 | when airplane.active = false then 'Inactive'
|
|---|
| 17 | when nf.departure < now() and nf.arrival > now() then 'In Flight'
|
|---|
| 18 | when lf.flightid is null then 'Available - No Flight History'
|
|---|
| 19 | when now() - lf.arrival < interval '4 hours' then 'Turnaround'
|
|---|
| 20 | else 'Available'
|
|---|
| 21 | end
|
|---|
| 22 | from airplane
|
|---|
| 23 | join airplanemodel on airplane.modelid = airplanemodel.id
|
|---|
| 24 | left join lateral (
|
|---|
| 25 | select f.id as flightid, f.flightnumber, f.departure, f.arrival,
|
|---|
| 26 | dap.code as origin, aap.code as destination
|
|---|
| 27 | from flight f
|
|---|
| 28 | join gate dg on dg.id = f.actualgatedepartureid
|
|---|
| 29 | join terminal dt on dt.id = dg.terminalid
|
|---|
| 30 | join airport dap on dap.id = dt.airportid
|
|---|
| 31 | join gate ag on ag.id = f.actualgatearrivalid
|
|---|
| 32 | join terminal at_ on at_.id = ag.terminalid
|
|---|
| 33 | join airport aap on aap.id = at_.airportid
|
|---|
| 34 | where f.airplaneid = airplane.id and f.departure < now()
|
|---|
| 35 | order by f.departure desc
|
|---|
| 36 | limit 1
|
|---|
| 37 | ) lf on true
|
|---|
| 38 | left join lateral (
|
|---|
| 39 | select f.id as flightid, f.flightnumber, f.departure, f.arrival,
|
|---|
| 40 | dap.code as origin, aap.code as destination
|
|---|
| 41 | from flight f
|
|---|
| 42 | join gate dg on dg.id = f.actualgatedepartureid
|
|---|
| 43 | join terminal dt on dt.id = dg.terminalid
|
|---|
| 44 | join airport dap on dap.id = dt.airportid
|
|---|
| 45 | join gate ag on ag.id = f.actualgatearrivalid
|
|---|
| 46 | join terminal at_ on at_.id = ag.terminalid
|
|---|
| 47 | join airport aap on aap.id = at_.airportid
|
|---|
| 48 | where f.airplaneid = airplane.id and f.departure > now()
|
|---|
| 49 | order by f.departure asc
|
|---|
| 50 | limit 1
|
|---|
| 51 | ) nf on true;
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|