Postgres SQL(Amazon Redshift) - 如何计算两个纬度和经度之间的距离?

Fra*_* P. 1 sql amazon-web-services amazon-redshift

在Netezza中,我习惯使用INZA包和ST_Distance函数.Redshift中是否有人知道类似的方式?我用它进行了大量的计算和连接.

小智 8

看一下这个!!

------- DISTANCE FUNCTION ---------
CREATE FUNCTION DISTANCE (orig_lat float, orig_long float, dest_lat float, dest_long float)
  RETURNS float
STABLE  
AS $$
  import math
  r = 3963.1676          
  phi_orig = math.radians(orig_lat)
  phi_dest = math.radians(dest_lat)
  delta_lat = math.radians(dest_lat - orig_lat)
  delta_long = math.radians(dest_long - orig_long)
  a = math.sin(delta_lat/2) * math.sin(delta_lat/2) + math.cos(phi_orig) \
      * math.cos(phi_dest) * math.sin(delta_long/2) * math.sin(delta_long/2)
  c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
  d = r * c
  return d
$$ LANGUAGE plpythonu
;
Run Code Online (Sandbox Code Playgroud)