SQL & DB/HackerRank SQL Problem

[HackerRank SQL] Weather Observation Station 19, 20(MySQL)

YSY^ 2021. 3. 1. 20:31

www.hackerrank.com/challenges/weather-observation-station-19/problem

 

Weather Observation Station 19 | HackerRank

Query the Euclidean Distance between two points and round to 4 decimal digits.

www.hackerrank.com

Answer

select round(sqrt(power(max(lat_n) - min(lat_n),2) + power(max(long_w) - min(long_w),2)),4)
from station

www.hackerrank.com/challenges/weather-observation-station-20/problem

 

Weather Observation Station 20 | HackerRank

Query the median of Northern Latitudes in STATION and round to 4 decimal places.

www.hackerrank.com

Answer - 중앙값 구현하기

MySQL은 중앙값을 지원하지 않아서 따로 구현해야 한다.

set @rownum = -1;
select round(avg(lat_n),4) as median
from (select @rownum := @rownum + 1 as rownumber, lat_n
     from station
     order by lat_n) as d
where rownumber in (ceil(@rownum / 2), floor(@rownum / 2))

참고로 오라클은 중앙값 기능을 지원한다!

 

728x90
반응형