forked from Weitzman-MUSA-GeoCloud/assignment02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery04.sql
More file actions
45 lines (42 loc) · 1.07 KB
/
Copy pathquery04.sql
File metadata and controls
45 lines (42 loc) · 1.07 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
with shape_lines as (
select
shape_id,
st_makeline(
array_agg(
st_setsrid(
st_makepoint(shape_pt_lon, shape_pt_lat),
4326
)::geography::geometry
order by shape_pt_sequence
)
)::geography as shape_geog
from septa.bus_shapes
group by shape_id
),
distinct_trips as (
select distinct
routes.route_short_name,
trips.trip_headsign,
trips.shape_id
from septa.bus_trips as trips
inner join septa.bus_routes as routes
on trips.route_id = routes.route_id
),
ranked as (
select
dt.route_short_name,
dt.trip_headsign,
round(st_length(shapes.shape_geog)::numeric) as shape_length,
row_number() over (
order by st_length(shapes.shape_geog) desc
) as rn
from distinct_trips as dt
inner join shape_lines as shapes
on dt.shape_id = shapes.shape_id
)
select
route_short_name,
trip_headsign,
shape_length
from ranked
where rn <= 2