0

Image with part of the database in question

I have this table

id ¦ Follow 
1 ¦ Route 1
2 ¦ Store 1
3 ¦ Store 2
4 ¦ end
5 ¦ Route 2
6 ¦ Store 3
7 ¦ Store 4
8 ¦ end
etc, etc

So, what I try to understand is how can I get only the rows starting from Route Number (i.e Route 2) until the first "end" is found? Routes are subject to change so it have to be from string "Route number" to the next "end" met.

The solution may be very simple but I have no clue where to start from, as I am quite new in this area and I haven't found anything relevant on SO. Please let me know if you want me to be more specific.

Thanks!

6

1 Answer 1

1

Let me assume that you have a column that specifies the ordering, uniquely. I'll call it id. Then you can do:

select t.*
from t cross join
     (select id
      from t
      where name = 'Route 2'
     ) tt
where t.id >= tt.id and
      t.id <= (select min(t2.id)
               from t t2
               where t2.id > t.id and
                     t2.name = 'end'
              );

I should note that you have a very arcane data structure. I think you might reconsider how the data is stored.

Sign up to request clarification or add additional context in comments.

2 Comments

@ceucaciprian This actually works if where t2.id > t.id is changed to where t2.id > tt.id - sqlfiddle.com/#!9/1c6f1/4
Wow! Thank you so much, Druv Saxena! And thank you again, Gordon!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.