MS SQL:
Table One: Routes –Table Two: Stops (Uses an int field [seq] to get the sequence of stops, key field ID)
How can I join [routes] to the [stops] table and return only the first and last route into a single row?
Let’s say I do a simple join:
Route 1 | Stop 1
Route 1 | Stop 2
Route 1 | Stop 3
Route 2 | Stop 1
Route 2 | Stop 2
I want it to return:
Route 1 | Stop 1 | Stop 3
Route 2 | Stop 1 | Stop 2
I can join them and get the Min(Seq) and Max(Seq), but not sure how I can get the ID and not Seq. It’s almost like I want to do two joins, but multiple fields. I need it to be efficient because of returning thousands of routes at a given time. So I don’t think a table valued function to get the stops would be beneficial. However, if I could get the min and max seq, returning the associated stop ID, I could simply join Stops to these to get the related data.
1 Answer