My goal is simple. In my app, the user would press on a stop and it would open a screen displaying each route (bus) that is going by that stop and display the next 2 stop times for each route at that stop. Example of what I want to display when user presses stop with stop id of 1571:
Buses traveling by stop 569
Route 2
23 minutes (7:23 AM)
43 minutes (7:43 AM)
Route 37
15 minutes (7:15 AM)
45 minutes (7:45 AM)
Example data returned (what I want):
st.departure_time, rte.route_long_name, tr.trip_headsign
07:23:00, Route 2, Downtown
07:43:00, Route 2, Downtown
07:15:00, Route 37, Downtown
07:45:00, Route 37, Downtown
It should be noted that the stop code is 569 and the stop id is 1571. As per GTFS documentation, a user would usually search the stop code (569) but internally the stop id (1571) is what is searched. These come from the stops.txt file in a GTFS package.
The example is displaying 2 routes (2 and 37) but there could be more routes, there could be only 1 route, or there could be no routes at all passing this stop in the near future (I want to ignore times that are more than 24 hours away).
Disclaimer: I am not so good with SQL. I was able to create a query that appears to retrieve relevant data, however, it returns way more data than just the next 2 stop times for each route. I am parsing it in my app to get the desired results but I feel like it would be a lot easier to just improve my query.
SELECT stop_times.departure_time, routes.route_long_name, trips.trip_headsign, calendar.monday, calendar.tuesday, calendar.wednesday, calendar.thursday, calendar.friday, calendar.saturday, calendar.sunday
FROM stop_times, trips, routes, calendar
WHERE stop_times.stop_id = '1571' AND stop_times.trip_id = trips.trip_id AND trips.route_id = routes.route_id AND trips.service_id = calendar.service_id AND 20180801 >= calendar.start_date AND 20180801 <= calendar.end_date
ORDER BY routes.route_short_name, trips.service_id, stop_times.departure_time
Here are the relevant tables. I store my data in database exactly as specified in the GTFS documentation.
stops.txt example rows:
routes.txt example rows:
trips.txt example rows:
stop_times.txt example rows:
calendar.txt example rows:
Thank you very much for any help.