I have the following database tables, which hold information about a public transport system:
- Stations (id, name)
- Ride(id, lineName)
- RideStop(rideId, stationId, sequenceNumber, arrivalTime, departureTime)
The RideStop has references to Ride and Station. When ordering the RideStops by sequenceNumber, you get the path of this specific ride.
I need to get the paths from all rides, which leave from a specific station.
I can get the complete paths by executing
select *
from ridestop rs
where rs.ride in (
SELECT ride FROM ttm.ridestop where stationid = 8503000
)
However, I do not care where a ride came from, only where it goes to.
Question:
How can I limit the result to only the remaining part of the rides, beginning at station 8503000?
The following query uses a subquery to the sequence number of each station on the ride. It then joins this back to the ride stop to choose everything from the ride on or after that sequence number.