요즘 스터디 해야겠다는 생각만 하고,
업무 외엔 다른 걸 안 해서 이렇게라도 스터디 기록을 남긴다
난 전공자 짬바가 있으니까 어려움만 풀었다 사실 다 풀기엔 귀찮음
폐쇄할 따릉이 대여소 찾기1
난이도 : 어려움
https://solvesql.com/problems/find-unnecessary-station-1/
⚠ 답변을 보고 싶지 않은 사람을 위해서 가림.
쿼리를 보고 싶은 분만 [더보기]를 클릭하세요
더보기
# 거리 계산은 sqlite distance between latitude and longitude 로 구글링해서 함수를 찾았다
# 원래 하는 업무의 특성 상 인접 지역에 특정 조건에 해당하는 지점을 찾는 케이스가 많아서 익숙했다. 엑셀로도 뽑아봄 (countifs 활용)
SELECT station_id, name
FROM (
SELECT s.station_id, s.name, count(s_near.station_id) as cnt
FROM station s # 기준 스테이션
LEFT JOIN station s_near # 인접 스테이션
ON s.station_id != s_near.station_id # 같은 스팟은 안 겹치게
AND s_near.updated_at > s.updated_at # 업데이트 시점 필터링
AND ( 6371 * acos( cos( radians(s.lat) ) * cos( radians( s_near.lat ) )
* cos( radians( s_near.lng ) - radians(s.lng) )
+ sin( radians(s.lat) ) * sin( radians( s_near.lat ) ) ) ) <= 0.3 # 거리계산
group by s.station_id
having cnt >= 5 # 인접 스팟의 개수 필터링 조건
)t
ORDER BY station_id # id 오름차순
'STUDY ✏️' 카테고리의 다른 글
프로그래머스 SQL 문제풀이 - Lv.4 (0) | 2022.10.09 |
---|