I'm writing an application that models train routes, which are stored in the database table [TrainStop] as follows:
TrainServiceId StationCode StopIndex IsEnabled
So a given train follows a route and consists of several rows with the StopIndex indicating the order. The problem I am trying to solve is to say which stations a user can get to from a given starting station based on the train services that are available. This would be relatively straightforward BUT it is also possible to disable stops which means that a user cannot get to any destinations after that stop for that train service. It is also possible that multiple trains on different routes can share stations e.g.:
Train Service 1: A, B, C, D, E
Train Service 2: P, Q, B, C, D, R
So if a user is at B they can go to C, D, E and R but if station D is disabled on either service they can get to C only. If station E is disabled on service 1 the user can get to C, D and R.
Solving this problem is fairly straightforward within C# but I am wondering whether it can be solved elegantly and efficiently within SQL? I'm struggling to find a way, for each route, to rule out stations past a row that is not enabled.