| 1 | create materialized view FlightCargoStatistics (FlightID, FlightNumber, Origin, Destination,
|
|---|
| 2 | TotalLuggage, TotalLuggageWeightKg, TotalCargo, TotalCargoWeightKg) as
|
|---|
| 3 |
|
|---|
| 4 | with 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 | ),
|
|---|
| 12 | CargoSummary as (
|
|---|
| 13 | select flightid,
|
|---|
| 14 | count(id) as TotalCargo,
|
|---|
| 15 | coalesce(sum(weight), 0) as TotalCargoWeightKg
|
|---|
| 16 | from additionalcargo
|
|---|
| 17 | group by flightid
|
|---|
| 18 | ),
|
|---|
| 19 | Airports 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 | )
|
|---|
| 30 | select 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)
|
|---|
| 35 | from Airports
|
|---|
| 36 | left join LuggageSummary on LuggageSummary.flightid = Airports.flightid
|
|---|
| 37 | left join CargoSummary on CargoSummary.flightid = Airports.flightid;
|
|---|
| 38 |
|
|---|