DatabaseCreation: FlightCargoStatistics.sql

File FlightCargoStatistics.sql, 1.4 KB (added by 231044, 7 days ago)
Line 
1create materialized view FlightCargoStatistics (FlightID, FlightNumber, Origin, Destination,
2 TotalLuggage, TotalLuggageWeightKg, TotalCargo, TotalCargoWeightKg) as
3
4with LuggageSummary as (
5 select t.flightid,
6 count(b.id) as TotalLuggage,
7 coalesce(sum(b.weight), 0) as TotalLuggageWeightKg
8 from ticket t
9 left join baggage b on b.ticketid = t.id
10 group by t.flightid
11),
12CargoSummary as (
13 select flightid,
14 count(id) as TotalCargo,
15 coalesce(sum(weight), 0) as TotalCargoWeightKg
16 from additionalcargo
17 group by flightid
18),
19Airports as (
20 select f.id as FlightID, f.flightnumber,
21 da.code as Origin, aa.code as Destination
22 from flight f
23 join gate dg on dg.id = f.actualgatedepartureid
24 join terminal dt on dt.id = dg.terminalid
25 join airport da on da.id = dt.airportid
26 join gate ag on ag.id = f.actualgatearrivalid
27 join terminal at_ on at_.id = ag.terminalid
28 join airport aa on aa.id = at_.airportid
29)
30select Airports.flightid, Airports.flightnumber, Airports.origin, Airports.destination,
31 coalesce(LuggageSummary.TotalLuggage, 0),
32 coalesce(LuggageSummary.TotalLuggageWeightKg, 0),
33 coalesce(CargoSummary.TotalCargo, 0),
34 coalesce(CargoSummary.TotalCargoWeightKg, 0)
35from Airports
36left join LuggageSummary on LuggageSummary.flightid = Airports.flightid
37left join CargoSummary on CargoSummary.flightid = Airports.flightid;
38