Last active
October 5, 2019 06:55
-
-
Save atronah/79e35176f2681a2bddbc65704eab4dbf to your computer and use it in GitHub Desktop.
PostgreSQL query for calculating possibility of flights between airports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select | |
-- `distinct on` statement is required to exclude duplicate lines with return flights | |
distinct on (least(depart.arport_code, arrival.airport_code) | |
, greatest(depart.arport_code, arrival.airport_code)) | |
depart.arport_name | |
, arrival.airport_name | |
, 6371 -- Earth radius | |
* acos(-- sin(latitude_a) * sin(latitude_b) | |
sin(depart.coordinates[0]) * sin(arriv.coordinates[1]) | |
-- cos(latitude_a) * cos(latitude_b) | |
+ cos(depart.coordinates[0]) * cos(arriv.coordinates[1]) | |
-- cos(longtitude_a - longtitude_b) | |
* cos(depart.coordinates[1] - arriv.coordinates[1]) | |
) as fact_range | |
, craft.range as craft_range | |
from flights as f | |
inner join airports_data as depart on depart.airport_code = f.departure_airport | |
inner join airports_data as arriv on arriv.airport_code = f.arrival_airport | |
inner join aircrafts_data as craft on craft.aircraft_code = f.aircraft_code | |
where | |
craft.range > (6371 -- Earth radius | |
* acos(-- sin(latitude_a) * sin(latitude_b) | |
sin(depart.coordinates[0]) * sin(arriv.coordinates[1]) | |
-- cos(latitude_a) * cos(latitude_b) | |
+ cos(depart.coordinates[0]) * cos(arriv.coordinates[1]) | |
-- cos(longtitude_a - longtitude_b) | |
* cos(depart.coordinates[1] - arriv.coordinates[1]) | |
)) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment