CodingSpace

[HackerRank/SQL] Aggregation - Weather Observation Station 18 본문

HackerRank/SQL

[HackerRank/SQL] Aggregation - Weather Observation Station 18

개발자_조이킴 2022. 8. 2. 09:17

Problem. Basic Select - Weather Observation Station 18


Link.

https://www.hackerrank.com/challenges/weather-observation-station-18/problem?isFullScreen=true 

 

Weather Observation Station 18 | HackerRank

Query the Manhattan Distance between two points, round or truncate to 4 decimal digits.

www.hackerrank.com


Description.

Consider P1(a, b) and P2(c, d) to be two points on a 2D plane.

  •  a happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
  •  b happens to equal the minimum value in Western Longitude (LONG_W in STATION).
  •  c happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
  •  d happens to equal the maximum value in Western Longitude (LONG_W in STATION).

Query the Manhattan Distance between points  and  and round it to a scale of 4 decimal places.

 

STATION 테이블


Key Point. 

Manhattan Distance

A(x1, y1)와 B(x2, y2)의 멘하탄 거리는

Manhattan Distance = |x1 - x2| + |y1 - y2|

 

ROUND,

 

ABS,

 

MIN,

 

MAX,

 


My Answer. 

SELECT
    ROUND(ABS(SS.a - SS.c) + ABS(SS.b - SS.d), 4)
FROM
(SELECT
    MIN(LAT_N) AS a,
    MIN(LONG_W) AS b,
    MAX(LAT_N) AS c,
    MAX(LONG_W) AS d
 FROM STATION
) AS SS;

References. 

 

Manhattan Distance: https://xlinux.nist.gov/dads/HTML/manhattanDistance.html

 

Manhattan distance

Definition: The distance between two points measured along axes at right angles. In a plane with p1 at (x1, y1) and p2 at (x2, y2), it is |x1 - x2| + |y1 - y2|. Note: This is easily generalized to higher dimensions. Manhattan distance is often used in inte

xlinux.nist.gov

 

Comments