GTFS Query to list all departure and arrival times between two stop names

Miro

I'm working with GTFS structure for the first time and am having trouble with the queries. I've got the transit data into mysql tables, and am free to query them, but I feel like I'm doing far too many queries and for loops to get the simplest info.

What I'd like to get in a single query is all the departure times and arrival times between two known stops, possibly identified by name.

Here is what I have so far, which involves a query, followed by looping through each trip_id to find the departure and arrival station info + times.

Query 1:

Show all departure times from a particular origin station, going in a particular direction. Result will give departure times and trip_ids.

SELECT t.trip_id, trip_headsign, departure_time, direction_id, s.stop_name 
FROM stops s, routes r, stop_times st, calendar c, trips t 
WHERE departure_time > "00:00:00" and departure_time < "23:59:59" 
AND r.route_id=1 and s.stop_id = 42 
AND s.stop_id = st.stop_id 
AND st.trip_id = t.trip_id   
AND c.service_id = t.service_id   
AND c.monday=1 and direction_id=1;

Result

+---------+---------------+----------------+--------------+-----------+
| trip_id | trip_headsign | departure_time | direction_id | stop_name |
+---------+---------------+----------------+--------------+-----------+
| 5671498 | Grand Central | 04:43:00       |            1 | Garrison  |
| 5671501 | Grand Central | 05:13:00       |            1 | Garrison  |
| 5671504 | Grand Central | 05:43:00       |            1 | Garrison  |
| 5671506 | Grand Central | 06:08:00       |            1 | Garrison  |
| 5671507 | Grand Central | 06:32:00       |            1 | Garrison  |
| 5671513 | Grand Central | 06:53:00       |            1 | Garrison  |
| 5671516 | Grand Central | 07:18:00       |            1 | Garrison  |
| 5671519 | Grand Central | 07:40:00       |            1 | Garrison  |
| 5671521 | Grand Central | 08:03:00       |            1 | Garrison  |
| 5671523 | Grand Central | 08:32:00       |            1 | Garrison  |
| 5671525 | Grand Central | 08:58:00       |            1 | Garrison  |
| 5671526 | Grand Central | 09:27:00       |            1 | Garrison  |
| 5671529 | Grand Central | 10:24:00       |            1 | Garrison  |
| 5671532 | Grand Central | 11:24:00       |            1 | Garrison  |
| 5671535 | Grand Central | 12:24:00       |            1 | Garrison  |
| 5671537 | Grand Central | 13:24:00       |            1 | Garrison  |
| 5671540 | Grand Central | 14:24:00       |            1 | Garrison  |
| 5671542 | Grand Central | 15:24:00       |            1 | Garrison  |
| 5671543 | Grand Central | 16:22:00       |            1 | Garrison  |
| 5671547 | Grand Central | 17:24:00       |            1 | Garrison  |
| 5671550 | Grand Central | 18:24:00       |            1 | Garrison  |
| 5671552 | Grand Central | 19:26:00       |            1 | Garrison  |
| 5671554 | Grand Central | 20:24:00       |            1 | Garrison  |
| 5671556 | Grand Central | 21:24:00       |            1 | Garrison  |
| 5671557 | Grand Central | 22:24:00       |            1 | Garrison  |
| 5671559 | Croton-Harmon | 23:24:00       |            1 | Garrison  |
+---------+---------------+----------------+--------------+-----------+

Query 2:

Give me all the stops on a particular trip along with their arrival times, use trip_id from last query:

SELECT s.stop_id,stop_lat, stop_lon, stop_name, arrival_time, stop_sequence 
FROM stop_times st JOIN stops s ON s.stop_id=st.stop_id 
WHERE trip_id=5671521;

Result

+---------+-----------+------------+------------------+--------------+---------------+
| stop_id | stop_lat  | stop_lon   | stop_name        | arrival_time | stop_sequence |
+---------+-----------+------------+------------------+--------------+---------------+
|      51 | 41.705839 | -73.937946 | Poughkeepsie     | 07:31:00     |             1 |
|      49 | 41.587448 | -73.947226 | New Hamburg      | 07:41:00     |             2 |
|      46 | 41.504007 | -73.984528 | Beacon           | 07:50:00     |             3 |
|      43 | 41.415283 | -73.958090 | Cold Spring      | 07:58:00     |             4 |
|      42 | 41.381780 | -73.947202 | Garrison         | 08:03:00     |             5 |
|      40 | 41.332601 | -73.970426 | Manitou          | 08:08:00     |             6 |
|      39 | 41.285962 | -73.930420 | Peekskill        | 08:17:00     |             7 |
|      37 | 41.246259 | -73.921884 | Cortlandt        | 08:22:00     |             8 |
|      33 | 41.189903 | -73.882394 | Croton-Harmon    | 08:32:00     |             9 |
|       4 | 40.805157 | -73.939149 | Harlem-125th St. | 09:09:00     |            10 |
|       1 | 40.752998 | -73.977056 | Grand Central    | 09:22:00     |            11 |
+---------+-----------+------------+------------------+--------------+---------------+

What I'd really like is a list of departure times and arrival times between two stops, such as this theorized result:

+---------+----------------+----------------+-----------+---------------+--------------+
| trip_id | departure_stop | departure_time | direction | arrival_stop  | arrival_time |
+---------+----------------+----------------+-----------+---------------+--------------+
| 5671521 | Garrison       | 08:03:00       |         1 | Grand Central | 09:22:00     |
| 5671522 | Garrison       | 08:32:00       |         1 | Grand Central | 09:51:00     |    
...etc...
Tin Tran

simply joining data together, joining to stops twice for start and end stop and joining to stop_times twice for start and end stop_times,
the only thing i am unsure about is where direction_id comes from.
try the below query.
At the very end of the query, you can specify start_s.stop_id and end_s.stop_id which represents the two stops you're querying data about.

SELECT t.trip_id,
       start_s.stop_name as departure_stop,
       start_st.departure_time,
       direction_id as direction,
       end_s.stop_name as arrival_stop,
       end_st.arrival_time
FROM
trips t INNER JOIN calendar c ON t.service_id = c.service_id
        INNER JOIN routes r ON t.route_id = r.route_id
        INNER JOIN stop_times start_st ON t.trip_id = start_st.trip_id
        INNER JOIN stops start_s ON start_st.stop_id = start_s.stop_id
        INNER JOIN stop_times end_st ON t.trip_id = end_st.trip_id
        INNER JOIN stops end_s ON end_st.stop_id = end_s.stop_id
WHERE c.monday = 1 
  AND direction_id = 1
  AND start_st.departure_time > "00:00:00" AND start_st.departure_time < "23:59:59" 
  AND r.route_id = 1
  AND start_s.stop_id = 42
  AND end_s.stop_id = 1

I tried looking up GTFS structure example from this link and i couldn't find anything on direction_id

To specify stop names instead of AND start_s.stop_id = 42 AND end_s.stop_id = 1
just use AND start_s.stop_name = 'Garrison' AND end_s.stop_name = 'Grand Central'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

SQL query for calculating arrival time of bus based on departure time, time from origin and stop order

From Dev

How to differentiate between arrival and departure timings to record in database

From Dev

List of times between two TimeSpans

From Dev

mySQL query between two dates and two times

From Dev

SQL query between two times on every day

From Dev

Is it possible to list the files between two names alphanumerically?

From Dev

SQL query to select data between two dates and times

From Dev

Java 8 LocalDateTime - How to Get All Times Between Two Dates

From Dev

How to add 'n' number of spaces between two Column names in Query?

From Dev

How to get list of times between two time in android?

From Dev

Get List of Records between two times C# Linq To Entities

From Dev

How to list all months between two dates

From Dev

List all days between two dates in Oracle

From Dev

Get a list of all tags between two commits

From Dev

PHP list all years between two dates

From Dev

List all days between two dates in Oracle

From Dev

Minutes between two Times

From Dev

Selecting between two times

From Java

How to list only the file names that changed between two commits?

From Dev

How to list only the names of files that changed between two commits?

From Dev

A query to list the names of all tables that contain a column with a specific name

From Dev

A query to list the names of all tables that contain a column with a specific name

From Dev

How can I list a stop's directly connected neighbors using GTFS?

From Dev

Data frame of departure and return dates, how do I get a list of all dates away?

From Dev

Query to fetch counts for all the dates between the two dates

From Dev

MySql query executes two times?

From Dev

Merging times between start and stop events in R

From Dev

Recording gaps between two times

From Dev

midpoint between two POSIXct times

Related Related

  1. 1

    SQL query for calculating arrival time of bus based on departure time, time from origin and stop order

  2. 2

    How to differentiate between arrival and departure timings to record in database

  3. 3

    List of times between two TimeSpans

  4. 4

    mySQL query between two dates and two times

  5. 5

    SQL query between two times on every day

  6. 6

    Is it possible to list the files between two names alphanumerically?

  7. 7

    SQL query to select data between two dates and times

  8. 8

    Java 8 LocalDateTime - How to Get All Times Between Two Dates

  9. 9

    How to add 'n' number of spaces between two Column names in Query?

  10. 10

    How to get list of times between two time in android?

  11. 11

    Get List of Records between two times C# Linq To Entities

  12. 12

    How to list all months between two dates

  13. 13

    List all days between two dates in Oracle

  14. 14

    Get a list of all tags between two commits

  15. 15

    PHP list all years between two dates

  16. 16

    List all days between two dates in Oracle

  17. 17

    Minutes between two Times

  18. 18

    Selecting between two times

  19. 19

    How to list only the file names that changed between two commits?

  20. 20

    How to list only the names of files that changed between two commits?

  21. 21

    A query to list the names of all tables that contain a column with a specific name

  22. 22

    A query to list the names of all tables that contain a column with a specific name

  23. 23

    How can I list a stop's directly connected neighbors using GTFS?

  24. 24

    Data frame of departure and return dates, how do I get a list of all dates away?

  25. 25

    Query to fetch counts for all the dates between the two dates

  26. 26

    MySql query executes two times?

  27. 27

    Merging times between start and stop events in R

  28. 28

    Recording gaps between two times

  29. 29

    midpoint between two POSIXct times

HotTag

Archive