데이터 분석/SQL 연습문제
Weather Observation Station 20 (HackerRank)
중급닌자 연습생
2022. 11. 7. 21:29
A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to decimal places.
풀이
제출한 답
with sub as (
select *, row_number() over (order by lat_n) rw, count(*) over () cnt
from station)
select round(avg(lat_n),4)
from sub
where case when mod(cnt,2) = 1 then rw = (cnt+1)/2
else rw in (cnt/2, (cnt/2)+1) end
-> 갯수는 cnt, 행 번호는 rw, 갯수가 짝수일 때는 두 값을 평균내야 함