I need to do a SELECT from the following two tables to the result table:
flight_connection
| flight number | departure | arrival |
|---|---|---|
| 310 | NUE | TXL |
| 926 | FRA | NUE |
| 312 | TXL | NUE |
airport
| code | description |
|---|---|
| NUE | Nuremberg |
| FRA | Frankfurt |
| TXL | Berlin-Tegel |
RESULT TABLE:
| code | description | departures | arrivals |
|---|---|---|---|
| NUE | Nuremberg | 1 | 2 |
| FRA | Frankfurt | 1 | 0 |
| TXL | Berlin-Tegel | 1 | 1 |
This is for counting all departures and arrivals for each airport and if one does not have a departure or arrival then it must be filled with 0 and not NULL.
I have so far gotten a table for either arrival or departure but not together, I've tried with UNION ALL but this just merges departures and arrivals count, I'm a little lost and would appreciate a little help.
Departure
SELECT DISTINCT code, description, Count(departure) AS departures FROM
(
SELECT code, description, departure FROM airport
INNER JOIN flight_connection ON departure = code
) as tmptbl
GROUP BY code, description
Arrival
SELECT DISTINCT code, description, Count(arrival) AS arrivals FROM
(
SELECT code, description, arrivalFROM airport
INNER JOIN flight_connection ON arrival = code
) as tmptbl
GROUP BY code, description