Euclidean Distance

匆匆过客 提交于 2020-04-21 05:02:14

问题


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.

+-------------+------------+
| Field       |   Type     |
+-------------+------------+
| ID          | INTEGER    |
| CITY        | VARCHAR(21)|
| STATE       | VARCHAR(2) |
| LAT_N       | NUMERIC    |
| LONG_W      | NUMERIC    |
+-------------+------------+

Consider P1(a, b) and P2(c, d) be two points on 2D plane, where (a, b) be minimum and maximum values of Northern Latitude and (c, d) be minimum and maximum values of Western Longitude. Write a query to print the Euclidean Distance between points P1 and P2 up to 4 decimal digits.

my solution for oracle is :

select round((abs(max(lat_n)-max(long_w)) + abs(min(long_w)-min(lat_n))),4) from station;  

but it is giving answer to the 3 decimal points but i need 4 decimal points.


回答1:


Try this ltrim(to_char(x, '999.9999'))

Number Format Models




回答2:


Try this SQL:

select
    cast(sqrt((power(max(lat_n)-min(lat_n),2))+(power(max(long_w)-in(long_w),2))) as decimal(16,4))
from station;



回答3:


select round(sqrt(((MAX(LAT_N) - MIN(LAT_N))*(MAX(LAT_N) - MIN(LAT_N))) 
+ ((MAX(LONG_W) - MIN(LONG_W))*(MAX(LONG_W) - MIN(LONG_W)))),4)

from station;

Try this in ORACLE SQL.




回答4:


It will work for sure.

DECLARE @a numeric(36,4),@b numeric(36,4),@c numeric(36,4),@d numeric(36,4)
SET @a=(SELECT MIN(CAST(ROUND(LAT_N,4) as numeric(36,4))) FROM STATION);
SET @b=(SELECT MIN(CAST(ROUND(LONG_W ,4) as numeric(36,4))) FROM STATION);
SET @c=(SELECT MAX(CAST(ROUND(LAT_N,4) as numeric(36,4))) FROM STATION);
SET @d=(SELECT MAX(CAST(ROUND(LONG_W ,4) as numeric(36,4))) FROM STATION);
SELECT DISTINCT CAST(ROUND((POWER(((POWER((@c-@a),2))+(POWER((@d-@b),2))),0.5)),4) as numeric(36,4)) FROM STATION



回答5:


Try this:

select round(sqrt(POWER((MAX(LAT_N) - MIN(LAT_N)),2) 
    + POWER((MAX(LONG_W) - MIN(LONG_W)),2)),4)
from station;



回答6:


select ltrim(to_char(sqrt(power(max(lat_n)-min(lat_n),2)+power(max(long_w)-min(long_w),2)),'999.9999')) from station;


来源:https://stackoverflow.com/questions/39892453/euclidean-distance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!