I have two tables:
AirPorts (IATA_CODE TEXT, LATITUDE REAL, LONGITUDE REAL)
AirRoutes (ROUTE_FROM TEXT, ROUTE_TO TEXT)
The AirRoutes table includes routes from Airports that are not listed in the Airports table. It also includes multiple occurrences of the same route.
First of all, I wish to select all unique Routes that start and end at airports that exist in the 'AirPorts' table. I don't know if this is the best way to do it, but it seems to work:
SELECT DISTINCT AirRoutes.ROUTE_FROM, AirRoutes.ROUTE_TO FROM AirRoutes
WHERE EXISTS
(SELECT IATA_CODE
FROM AirPorts_SM
WHERE AirRoutes.ROUTE_FROM = IATA_CODE)
AND EXISTS
(SELECT IATA_CODE
FROM AirPorts_SM
WHERE AirRoutes.ROUTE_TO = IATA_CODE);
Next, I would like to filter out B-A routes where A-B routes are returned, so if there is a route AMS to DUB, I don't want to return DUB to AMS as well.
How would you do that?
Finally, and I don't know if this is possible, I would like to get the positions included, so each row would be like:
ROUTE_FROM=AMS, LAT_FROM=52.3, LON_FROM=4.76, ROUTE_TO=DUB, LAT_TO=53.42, LON_TO=-6.24
How would you do that? Perhaps this is too adventurous for one query, and I should break it into three parts?