Prepare > SQL > Aggregation > Weather Observation Station 19
2023. 5. 22. 20:18ㆍHackerRank-My SQL
Weather Observation Station 19 | HackerRank
Query the Euclidean Distance between two points and round to 4 decimal digits.
www.hackerrank.com
문제
Consider p1(a,c) and p2(b,d) to be two points on a 2D plane where (a,b) are the respective minimum and maximum values of Northern Latitude (LAT_N) and (c,d) are the respective minimum and maximum values of Western Longitude (LONG_W) in STATION.
Query the Euclidean Distance between points p1 and p2 and format your answer to display 4 decimal digits.
=> p1 과 p2 의 유클리드 거리를 쿼리하라.
코드
SELECT TRUNCATE(SQRT((b - a) * (b - a) + (d - c) * (d - c)), 4)
FROM
(SELECT
MIN(LAT_N) AS a,
MAX(LAT_N) AS b,
MIN(LONG_W) AS c,
MAX(LONG_W) AS d
FROM STATION) AS K;
- a, b, c, d를 명확히 지정하는 서브쿼리 이용.(추후 문제에 이용할 작정)
- SQRT(~~~): 제곱근을 구해줌.
노트
POWER(~~, n): ~~의 n 제곱 값
SELECT TRUNCATE(SQRT(POWER(a - b, 2) + POWER(c - d, 2)), 4)
FROM
(SELECT
MIN(LAT_N) AS a,
MAX(LAT_N) AS b,
MIN(LONG_W) AS c,
MAX(LONG_W) AS d
FROM STATION) AS K;
SELECT TRUNCATE(SQRT(POWER(MIN(LAT_N) - MAX(LAT_N),2) + POWER(MIN(LONG_W) - MAX(LONG_W),2)),4)
FROM STATION;
두 번째 시도
SELECT ROUND(SQRT(POWER(MIN(LAT_N) - MAX(LAT_N), 2) + POWER(MIN(LONG_W) - MAX(LONG_W),2)),4) FROM STATION;
- 그냥 한 줄에 다 때려박음
'HackerRank-My SQL' 카테고리의 다른 글
Prepare > SQL > Basic Join > African Cities (0) | 2023.05.24 |
---|---|
Prepare > SQL > Advanced Select > Binary Tree Nodes (0) | 2023.05.23 |
Prepare > SQL > Aggregation > Weather Observation Station 18 (0) | 2023.05.20 |
Prepare > SQL > Aggregation > Weather Observation Station 17 (0) | 2023.05.18 |
Prepare > SQL > Aggregation > Weather Observation Station 16 (0) | 2023.05.18 |