在AWS Athena / Presto中的Radians函数中使用SQL数组

时间:2020-03-31 18:29:13

标签: sql distance presto amazon-athena

我正在尝试通过AWS Athena中的radians()函数传递一组经纬度坐标,但是会出错

Unexpected parameters (array(double)) for function radians. Expected: radians(double)

我需要能够通过lat_array和long_array传递数组,以检查每组坐标是否在my_table中设置的每个经纬度的1英里之内。

WITH
target_addresses AS (
SELECT ARRAY[CAST(lat AS real)] AS lat,
       ARRAY[CAST(long AS real)] AS long
FROM addresses
)

SELECT user_id, 
       ( 3959 * 
         ACOS( 
         COS( RADIANS( lat_array ) ) * 
         COS( RADIANS( latitude ) ) * 
         COS( RADIANS( longitude ) - RADIANS( long_array ) ) + 
         SIN( RADIANS( lat_array ) ) * 
         SIN( RADIANS( latitude) ) 
        ) 
       ) AS distance 
FROM 
 my_table,
 (SELECT array_agg(la) AS lat_array,
         array_agg(lo) AS long_array 
  FROM target_addresses
  CROSS JOIN UNNEST(lat) AS t(la)
  CROSS JOIN UNNEST(long) AS t(lo)) target_params
HAVING distance <= 1
ORDER BY distance ASC;

是否有更好的查询方法来实现此结果?

0 个答案:

没有答案