forked from Weitzman-MUSA-GeoCloud/assignment02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery07.sql
More file actions
39 lines (36 loc) · 1.03 KB
/
Copy pathquery07.sql
File metadata and controls
39 lines (36 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
WITH stop_trip_access AS (
SELECT DISTINCT
st.stop_id,
t.wheelchair_accessible
FROM septa.bus_trips t
JOIN septa.stop_times st
ON t.trip_id = st.trip_id
),
neighborhood_stops AS (
SELECT
n.name AS neighborhood_name,
s.stop_id,
sta.wheelchair_accessible
FROM phl.neighborhoods n
JOIN septa.bus_stops s
ON ST_Intersects(n.geog, s.geog)
LEFT JOIN stop_trip_access sta
ON s.stop_id = sta.stop_id
),
aggregated AS (
SELECT
neighborhood_name,
COUNT(*) FILTER (WHERE wheelchair_accessible = 1) AS num_bus_stops_accessible,
COUNT(*) FILTER (WHERE wheelchair_accessible IS DISTINCT FROM 1) AS num_bus_stops_inaccessible,
COUNT(*) AS total_stops
FROM neighborhood_stops
GROUP BY neighborhood_name
)
SELECT
neighborhood_name,
(num_bus_stops_accessible::float / total_stops) AS accessibility_metric,
num_bus_stops_accessible,
num_bus_stops_inaccessible
FROM aggregated
ORDER BY accessibility_metric ASC
LIMIT 5;