-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Cities | ||
|
||
https://dev.to/dm8ry/postgresql-how-to-calculate-the-distance-between-two-cities-based-on-their-corresponding-gps-data-23cm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CREATE TABLE cities ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(100), | ||
latitude NUMERIC, | ||
longitude NUMERIC | ||
); | ||
|
||
INSERT INTO cities (name, latitude, longitude) VALUES ('Lisbon', 38.724874, -9.139604); | ||
INSERT INTO cities (name, latitude, longitude) VALUES ('Porto', 41.158389, -8.629163); | ||
INSERT INTO cities (name, latitude, longitude) VALUES ('Sintra', 38.800306, -9.379136); | ||
INSERT INTO cities (name, latitude, longitude) VALUES ('Obidos', 39.362068, -9.157140); | ||
INSERT INTO cities (name, latitude, longitude) VALUES ('Coimbra', 40.211491, -8.429200); | ||
INSERT INTO cities (name, latitude, longitude) VALUES ('Covilha', 40.282650, -7.503260); | ||
INSERT INTO cities (name, latitude, longitude) VALUES ('Fatima', 39.617207, -8.652142); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
SELECT ST_DistanceSphere( | ||
ST_MakePoint(city1.longitude, city1.latitude), | ||
ST_MakePoint(city2.longitude, city2.latitude) | ||
) / 1000 AS distance_in_km | ||
FROM cities AS city1 | ||
CROSS JOIN cities AS city2 | ||
WHERE city1.name = 'Lisbon' | ||
AND city2.name = 'Porto'; | ||
|
||
SELECT ST_DistanceSphere( | ||
ST_MakePoint(city1.longitude, city1.latitude), | ||
ST_MakePoint(city2.longitude, city2.latitude) | ||
) / 1000 AS distance_in_km | ||
FROM cities AS city1 | ||
CROSS JOIN cities AS city2 | ||
WHERE city1.name = 'Sintra' | ||
AND city2.name = 'Coimbra'; | ||
|
||
SELECT city1.name "from", city2.name "to", round(ST_DistanceSphere( | ||
ST_MakePoint(city1.longitude, city1.latitude), | ||
ST_MakePoint(city2.longitude, city2.latitude) | ||
) / 1000) AS distance_in_km | ||
FROM cities AS city1 | ||
CROSS JOIN cities AS city2 | ||
where city1.name > city2.name | ||
order by 3 desc, 1, 2; | ||
|
||
|