Skip to content

Instantly share code, notes, and snippets.

@sonkol
Created October 9, 2024 14:33
Show Gist options
  • Save sonkol/ee6ff89ce52821b226853af52bac0ad5 to your computer and use it in GitHub Desktop.
Save sonkol/ee6ff89ce52821b226853af52bac0ad5 to your computer and use it in GitHub Desktop.
Given a stop from which connections departure, return a list of following stops
-- Query GTFS tables
SELECT DISTINCT
x.stop_id,
next_stop,
stops.stop_name
FROM
(
SELECT
stop_id,
stop_sequence,
LEAD (stop_id) OVER (PARTITION BY trip_id ORDER BY CAST(stop_sequence AS INTEGER)) next_stop
FROM stop_times
) AS x
JOIN
stops on next_stop = stops.stop_id
WHERE -- comment these lines
x.stop_id = "U100Z1P" -- to get a complete set of pairs
ORDER BY
x.stop_id, next_stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment