-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathL612_Shortest Distance in a Plane.sql
More file actions
49 lines (31 loc) · 1.03 KB
/
L612_Shortest Distance in a Plane.sql
File metadata and controls
49 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Table point_2d holds the coordinates (x,y) of some unique points (more than two) in a plane.
Write a query to find the shortest distance between these points rounded to 2 decimals.
| x | y |
|----|----|
| -1 | -1 |
| 0 | 0 |
| -1 | -2 |
The shortest distance is 1.00 from point (-1,-1) to (-1,2). So the output should be:
| shortest |
|----------|
| 1.00 |
select * from point_2d;
SELECT Round(SQRT(MIN(POW(p1.x-p2.x,2)+POW(p1.y-p2.y,2))),2) as shortest
FROM point_2d as p1
JOIN point_2d as p2
ON p1.x != p2.x OR p1.y != p2.y;
# avoid to calculate the duplicated one
SELECT
ROUND(SQRT(MIN((POW(p1.x - p2.x, 2) + POW(p1.y - p2.y, 2)))),2) AS shortest
FROM
point_2d p1
JOIN
point_2d p2 ON (p1.x <= p2.x AND p1.y < p2.y)
OR (p1.x <= p2.x AND p1.y > p2.y)
OR (p1.x < p2.x AND p1.y = p2.y);
/*
create table point_2d (x int, y int);
insert into point_2d values (-1,-1);
insert into point_2d values (0,0);
insert into point_2d values (-1,-2);
*/