SQL/Solve SQL

[solvesql] 폐쇄할 따릉이 대여소 찾기

소금깨 2022. 6. 14. 23:26
WITH sub_table AS (
  SELECT 
    t1.station_id AS station_id,
    t1.name AS name,
    2 * 6356 * asin(sqrt(pow(sin((radians(t1.lat) - radians(t2.lat)) / 2 ), 2) +
    cos(radians(t1.lat)) * cos(radians(t2.lat)) *
    pow(sin((radians(t1.lng) - radians(t2.lng)) / 2), 2))) AS distance
  FROM station t1
  INNER JOIN station t2 on 1=1
  WHERE t1.updated_at < t2.updated_at)
  
SELECT station_id
        ,name
        ,max(distance) 
FROM sub_table 
WHERE distance <= 0.3 
GROUP BY station_id,name
having count(*) >= 5