Euclidean Distance
Hacker Rank - Weather Observation Station 19 Given a table STATION that holds data for five fields namely ID, CITY, STATE, NORTHERN LATITUDE and WESTERN LONGITUDE. +------------
Solution 1:
This worked for me:
Select round(sqrt(power((min(lat_n)-max(lat_n)),2) +power((min(long_w)-max(long_w)),2)),4) From station
Solution 2:
For MySql it works fine:
SELECT ROUND(SQRT(POW(MIN(LAT_N)-MAX(LAT_N),2)+POW(MIN(LONG_W)-MAX(LONG_W),2)),4) FROM STATION;
Solution 3:
For SQL Server, try like this:
SELECTcast(
round(
SQRT(
SQUARE((Abs ((MIN(LAT_N ) -MAX(LAT_N )))) )
+ SQUARE( (Abs((MIN(LONG_W ) -MAX(LONG_W )))))
)
,4)
asdecimal(18,4)
)
FROM STATION
Solution 4:
MySQL soln:
select round( power( power((min(lat_n)-max(lat_n)),2) + power((min(long_w)-max(long_w)),2),1/2),4) as distance from station;
in this code I have used power(x,1/2) instead of sqrt(x) ..so that when needed to find nth root in future we can use power(x, 1/y) operation ( where y = 0,1,2,..,n).
Solution 5:
Try this ltrim(to_char(x, '999.9999'))
Post a Comment for "Euclidean Distance"