-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspatial_data.sql
More file actions
111 lines (106 loc) · 2.46 KB
/
Copy pathspatial_data.sql
File metadata and controls
111 lines (106 loc) · 2.46 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
-- =====================================================
-- Which customers are in the Southern Hemisphere?
-- =====================================================
SELECT
customernumber,
customername,
public.st_y (customerlocation) AS latitude
FROM
customers
WHERE
st_y (customerlocation) < 0;
-- =====================================================
-- Which US customers are south west of the New York office?
-- =====================================================
WITH
ny AS (
SELECT
officelocation AS geom
FROM
offices
WHERE
city ILIKE 'nyc'
)
SELECT
c.customernumber,
c.customername,
st_astext (c.customerlocation) AS location
FROM
customers c
CROSS JOIN ny
WHERE
c.country = 'USA'
AND st_y (c.customerlocation) < st_y (ny.geom)
AND st_x (c.customerlocation) < st_x (ny.geom);
-- =====================================================
-- Which customers are closest to the Tokyo office?
-- =====================================================
WITH
tokyo AS (
SELECT
officelocation AS geom
FROM
offices
WHERE
city ILIKE 'tokyo'
)
SELECT
c.customernumber,
c.customername,
st_astext (c.customerlocation) AS location
FROM
customers c
CROSS JOIN tokyo
ORDER BY
c.customerlocation <-> tokyo.geom;
-- =====================================================
-- Which French customer is furthest from the Paris office?
-- =====================================================
WITH
paris AS (
SELECT
officelocation AS geom
FROM
offices
WHERE
city = 'Paris'
)
SELECT
c.customernumber,
c.customername,
st_astext (c.customerlocation) AS location,
st_distance (c.customerlocation::geography, paris.geom::geography) AS distance_meters
FROM
customers c
CROSS JOIN paris
WHERE
c.country = 'France'
ORDER BY
distance_meters DESC
LIMIT
1;
-- =====================================================
-- Who is the northernmost customer?
-- =====================================================
SELECT
customernumber,
customername,
st_astext (customerlocation) AS location,
st_y (customerlocation) AS latitude
FROM
customers
ORDER BY
st_y (customerlocation) DESC
LIMIT
1;
-- =====================================================
-- What is the distance between the Paris and Boston offices?
-- =====================================================
SELECT
st_distance (p1.officelocation::geography, p2.officelocation::geography) / 1000 AS distance_km
FROM
offices p1
CROSS JOIN offices p2
WHERE
p1.city = 'Paris'
AND p2.city = 'Boston';