I have a database of all airports and need a query that finds the shortest distance between airports in 2 locations (cities for example). Here is my query.
SELECT
a.city,
a.iata_faa,
md.min_distance
FROM
airports as a, (
SELECT
aa.point AS pointa,
ab.point AS pointb,
min(st_distance(aa.point,ab.point)) AS min_distance
FROM
airports AS aa,
airports AS ab
WHERE
aa.city = 'Miami' AND ab.city = 'Atlanta'
GROUP BY 1,2
) AS md
WHERE
st_distance(a.pointa, md.pointb) = md.min_distance
;
I'm closer and tried the <-> operator in the ORDER BY clause but what I'm getting is and ambiguous column reference:
ERROR: column reference "point" is ambiguous
LINE 23: st_distance(a.point, md.point) = md.min_distance
Revised query:
SELECT
a.city,
b.city,
md.min_distance
FROM
airports AS a, airports AS b,
(
SELECT
aa.point,
ab.point,
min(st_distance(aa.point,ab.point)) AS min_distance
FROM
airports AS aa,
airports AS ab
WHERE
aa.city = 'Miami' AND ab.city = 'Atlanta'
GROUP BY 1,2
) AS md
WHERE
a.city = 'Miami' AND b.city = 'Atlanta' AND
st_distance(a.point, md.point) = md.min_distance
GROUP BY 1,2
ORDER BY a.point <-> b.point::geometry limit 1;
;