𝙎𝙌𝙇

[Solvesql] 폐쇄할 따릉이 정류소 찾기 1

콜라맛갈비 2023. 3. 30. 13:43
728x90

https://solvesql.com/problems/find-unnecessary-station-1/

 

solvesql

 

solvesql.com

 

조건1) 업무를 수행하기 위해 정류소 정보가 저장된 station 테이블을 사용

      --> from station 이용

 

조건2) 반경 300m 이내에

      --> 거리 구한 식이 0.3보다 작은 것

 

조건3) 나중에 생기거나 업그레이드 된 다른 정류소가

      --> 기준 updated_at이 더 작은 것

 

조건4) 5개 이상 있는 따릉이 대여소

      --> having절에 count값이 5개 이상인 것

 

조건5) 아이디(station_id)와 이름(name)을 출력하는 쿼리를 작성

      --> station_id, name 추출

 

 

select
  a.station_id,
  a.name
from
  station a
  join station b on a.station_id != b.station_id
  and a.updated_at < b.updated_at
  and (
    6371 * acos(
      cos(radians(a.lat)) * cos(radians(b.lat)) * cos(radians(b.lng) - radians(a.lng)) + sin(radians(a.lat)) * sin(radians(b.lat))
    )
  ) < 0.3
group by
  a.station_id
having
  count(a.station_id) >= 5
728x90