Problem Statement:
Consider P1(a,b) and P2(c,d) 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 P1 and P2 and round it to a scale of 4 decimal places.
Manhattan Distance: 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|.
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
Assume the STATION table has the following data:
The Manhattan Distance between the two points is computed as:
ABS(14.000 - 8.000) + ABS(24.000 - 18.000) = 12.000
The solution applies the concept of Manhattan Distance to find the distance between two points on a 2D plane.
Manhattan Distance is the sum of the absolute differences of the coordinates of two points. It is calculated by adding the absolute differences between the x-coordinates and y-coordinates of the two points.
In this problem, we need to find the Manhattan Distance between two points P1(a,b) and P2(c,d) where:
The SQL query written uses the MIN and MAX functions to retrieve the minimum and maximum values of latitude and longitude from the STATION table.
The ABS function is then used to calculate the absolute differences between the latitude and longitude values of the two points, and the ROUND function is used to round the result to 4 decimal places.
SELECT
ROUND(
ABS(
(SELECT MIN(LAT_N) FROM STATION) -
(SELECT MAX(LAT_N) FROM STATION)
)
+
ABS(
(SELECT MIN(LONG_W) FROM STATION) -
(SELECT MAX(LONG_W) FROM STATION)
),
4);
The output of the query is the Manhattan Distance between the two points, rounded to 4 decimal places.
I hope this works for you. Let me know if you face any issue in the comment section.