612_Shortest Distance in a Plane
| x | y |
|----|----|
| -1 | -1 |
| 0 | 0 |
| -1 | -2 || shortest |
|----------|
| 1.00 |Solution 1: using join
SELECT round(sqrt(min(power(p1.x-p2.x, 2) + power(p1.y-p2.y, 2))), 2) AS shortest
FROM point_2d p1 JOIN point_2d p2 ON (p1.x, p1.y) != (p2.x, p2.y);Solution 2: using subquery
SELECT min(dist) AS shortest
FROM (
SELECT p.x, p.y,
(SELECT round(min(sqrt(power(p.x - x, 2) + power(p.y - y, 2))), 2)
FROM point_2d
WHERE (x, y) != (p.x, p.y)) AS dist
FROM point_2d AS p
) AS t;Last updated