| 1 | create view CrewAvailability (EmployeeId, Name, Surname, DateOfBirth, Active, EmployeeType, Rank,
|
|---|
| 2 | LastFlightId, LastFlightNumber, LastFlightOrigin, LastFlightDestination,
|
|---|
| 3 | LastFlightDeparture, LastFlightArrival,
|
|---|
| 4 | NextFlightId, NextFlightNumber, NextFlightOrigin, NextFlightDestination,
|
|---|
| 5 | NextFlightDeparture, NextFlightArrival,
|
|---|
| 6 | HoursSinceLastFlight, HoursUntilNextFlight, Availability) as
|
|---|
| 7 | select pilot.employeeid, employee.name, employee.surname, employee.dateofbirth, employee.active,
|
|---|
| 8 | 'Pilot', pilotrank.name,
|
|---|
| 9 | lf.flightid, lf.flightnumber, ldap.code, laap.code, lf.departure, lf.arrival,
|
|---|
| 10 | nf.flightid, nf.flightnumber, ndap.code, naap.code, nf.departure, nf.arrival,
|
|---|
| 11 | extract(epoch from (now() - lf.arrival)) / 3600,
|
|---|
| 12 | extract(epoch from (nf.departure - now())) / 3600,
|
|---|
| 13 | case
|
|---|
| 14 | when lf.arrival > now() then 'On Duty'
|
|---|
| 15 | when now() - lf.arrival < interval '12 hours' then 'Resting'
|
|---|
| 16 | when lf.flightid is null then 'Available - No Flight History'
|
|---|
| 17 | else 'Available'
|
|---|
| 18 | end
|
|---|
| 19 | from pilot
|
|---|
| 20 | join employee on employee.id = pilot.employeeid
|
|---|
| 21 | join pilotrank on pilot.rank = pilotrank.id
|
|---|
| 22 | left join lateral (
|
|---|
| 23 | select id as flightid, flightnumber, departure, arrival,
|
|---|
| 24 | actualgatedepartureid as gdep, actualgatearrivalid as garr
|
|---|
| 25 | from (
|
|---|
| 26 | (select f.id, f.flightnumber, f.departure, f.arrival, f.actualgatedepartureid, f.actualgatearrivalid
|
|---|
| 27 | from flight f where f.pilot = pilot.employeeid and f.arrival < now()
|
|---|
| 28 | order by f.arrival desc limit 1)
|
|---|
| 29 | union all
|
|---|
| 30 | (select f.id, f.flightnumber, f.departure, f.arrival, f.actualgatedepartureid, f.actualgatearrivalid
|
|---|
| 31 | from flight f where f.copilot = pilot.employeeid and f.arrival < now()
|
|---|
| 32 | order by f.arrival desc limit 1)
|
|---|
| 33 | ) c
|
|---|
| 34 | order by arrival desc limit 1
|
|---|
| 35 | ) lf on true
|
|---|
| 36 | join gate ldg on ldg.id = lf.gdep
|
|---|
| 37 | join terminal ldt on ldt.id = ldg.terminalid
|
|---|
| 38 | join airport ldap on ldap.id = ldt.airportid
|
|---|
| 39 | join gate lag on lag.id = lf.garr
|
|---|
| 40 | join terminal lat_ on lat_.id = lag.terminalid
|
|---|
| 41 | join airport laap on laap.id = lat_.airportid
|
|---|
| 42 | left join lateral (
|
|---|
| 43 | select id as flightid, flightnumber, departure, arrival,
|
|---|
| 44 | actualgatedepartureid as gdep, actualgatearrivalid as garr
|
|---|
| 45 | from (
|
|---|
| 46 | (select f.id, f.flightnumber, f.departure, f.arrival, f.actualgatedepartureid, f.actualgatearrivalid
|
|---|
| 47 | from flight f where f.pilot = pilot.employeeid and f.departure > now()
|
|---|
| 48 | order by f.departure asc limit 1)
|
|---|
| 49 | union all
|
|---|
| 50 | (select f.id, f.flightnumber, f.departure, f.arrival, f.actualgatedepartureid, f.actualgatearrivalid
|
|---|
| 51 | from flight f where f.copilot = pilot.employeeid and f.departure > now()
|
|---|
| 52 | order by f.departure asc limit 1)
|
|---|
| 53 | ) c
|
|---|
| 54 | order by departure asc limit 1
|
|---|
| 55 | ) nf on true
|
|---|
| 56 | join gate ndg on ndg.id = nf.gdep
|
|---|
| 57 | join terminal ndt on ndt.id = ndg.terminalid
|
|---|
| 58 | join airport ndap on ndap.id = ndt.airportid
|
|---|
| 59 | join gate nag on nag.id = nf.garr
|
|---|
| 60 | join terminal nat_ on nat_.id = nag.terminalid
|
|---|
| 61 | join airport naap on naap.id = nat_.airportid
|
|---|
| 62 |
|
|---|
| 63 | union all
|
|---|
| 64 |
|
|---|
| 65 | select flightattendant.employeeid, employee.name, employee.surname, employee.dateofbirth, employee.active,
|
|---|
| 66 | 'Flight Attendant', flightattendant.certificationlevel::text,
|
|---|
| 67 | lf.flightid, lf.flightnumber, ldap.code, laap.code, lf.departure, lf.arrival,
|
|---|
| 68 | nf.flightid, nf.flightnumber, ndap.code, naap.code, nf.departure, nf.arrival,
|
|---|
| 69 | extract(epoch from (now() - lf.arrival)) / 3600,
|
|---|
| 70 | extract(epoch from (nf.departure - now())) / 3600,
|
|---|
| 71 | case
|
|---|
| 72 | when lf.arrival > now() then 'On Duty'
|
|---|
| 73 | when now() - lf.arrival < interval '12 hours' then 'Resting'
|
|---|
| 74 | when lf.flightid is null then 'Available - No Flight History'
|
|---|
| 75 | else 'Available'
|
|---|
| 76 | end
|
|---|
| 77 | from flightattendant
|
|---|
| 78 | join employee on employee.id = flightattendant.employeeid
|
|---|
| 79 | left join lateral (
|
|---|
| 80 | select f.id as flightid, f.flightnumber, f.departure, f.arrival,
|
|---|
| 81 | f.actualgatedepartureid as gdep, f.actualgatearrivalid as garr
|
|---|
| 82 | from cabincrew cc
|
|---|
| 83 | join flight f on f.id = cc.flightid
|
|---|
| 84 | where cc.flightattendantid = flightattendant.employeeid
|
|---|
| 85 | and f.arrival < now()
|
|---|
| 86 | order by f.arrival desc limit 1
|
|---|
| 87 | ) lf on true
|
|---|
| 88 | join gate ldg on ldg.id = lf.gdep
|
|---|
| 89 | join terminal ldt on ldt.id = ldg.terminalid
|
|---|
| 90 | join airport ldap on ldap.id = ldt.airportid
|
|---|
| 91 | join gate lag on lag.id = lf.garr
|
|---|
| 92 | join terminal lat_ on lat_.id = lag.terminalid
|
|---|
| 93 | join airport laap on laap.id = lat_.airportid
|
|---|
| 94 | left join lateral (
|
|---|
| 95 | select f.id as flightid, f.flightnumber, f.departure, f.arrival,
|
|---|
| 96 | f.actualgatedepartureid as gdep, f.actualgatearrivalid as garr
|
|---|
| 97 | from cabincrew cc
|
|---|
| 98 | join flight f on f.id = cc.flightid
|
|---|
| 99 | where cc.flightattendantid = flightattendant.employeeid
|
|---|
| 100 | and f.departure > now()
|
|---|
| 101 | order by f.departure asc limit 1
|
|---|
| 102 | ) nf on true
|
|---|
| 103 | join gate ndg on ndg.id = nf.gdep
|
|---|
| 104 | join terminal ndt on ndt.id = ndg.terminalid
|
|---|
| 105 | join airport ndap on ndap.id = ndt.airportid
|
|---|
| 106 | join gate nag on nag.id = nf.garr
|
|---|
| 107 | join terminal nat_ on nat_.id = nag.terminalid
|
|---|
| 108 | join airport naap on naap.id = nat_.airportid;
|
|---|
| 109 |
|
|---|