Query to work out possible destinations on a set of routes using SQL Server 2008

Posted by RobW on Stack Overflow See other posts from Stack Overflow or by RobW
Published on 2010-04-23T12:59:49Z Indexed on 2010/04/23 13:03 UTC
Read the original article Hit count: 174

Filed under:
|

I'm writing an application that models train routes, which are stored in the database table [TrainStop] as follows:

RouteId StationCode StopIndex IsEnabled

So a given route 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. 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. It is also possible that multiple routes can share stations e.g.:

Route 1: A, B, C, D, E

Route2: 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 they can get to C only.

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.

© Stack Overflow or respective owner

Related posts about sql-server-2008

Related posts about tsql