Here is my table:
ROUTES = the route ID
STATIONS = the station ID
STOPS? = if the train stops at this station then is equal to 1 otherwise 0
-------------------------
ROUTES STATIONS STOPS?
-------------------------
R1 S1 1
R1 S2 0
R1 S3 1
R1 S4 0
R1 S5 1
R2 S1 1
R2 S2 1
R2 S3 1
R2 S4 0
R2 S5 1
R3 S1 1
R3 S2 0
R3 S4 1
R3 S5 0
R3 S6 1
R3 S7 1
R4 S1 1
R4 S2 1
R4 S3 0
R4 S4 1
R5 S2 1
R5 S3 0
R5 S4 1
What I am trying to do is to find which routes pass through the same stations but don’t have the same stops.
For example:
We see that
Route R1 passes through stations S1->S2->S3->S4->S5
Route R2 passes through stations S1->S2->S3->S4->S5
but they have different stops, so the result should be:
R1
R2
I thought to group first all the ROUTES and compare the STATIONS belonging to that group with all the others and check if they have at least one different stop.
I assumed a TrainRoutes table with one row for each of R1, R2 etc. You could replace this with select distinct RouteID from Stops if required.
You can also do this without the TrainRoute table like so, but you’re now cross joining two larger tables:
http://sqlfiddle.com/#!6/76978/8