lassence.net

Field notes from the AI frontier

Compute Haversine distance in Redshift

--- archive: codebook, 2020–2023 · topic: SQL ---

Here is how you can calculate a Haversine distance in meters in Redshift, from latitudes and longitudes:

SELECT
  ST_DistanceSphere(
      ST_GeomFromText('POINT(' || longitude_1 || ' ' || latitude_1 || ')'),
      ST_GeomFromText('POINT(' || longitude_2 || ' ' || latitude_2 || ')')
  ) AS distance_in_meters
FROM (
  SELECT
    2.342981 AS longitude_1,
    48.861457 AS latitude_1,
    2.370203 AS longitude_2,
    48.899455 AS latitude_2
)