SQL - Finding continuous entries of a given size.

Posted by ByteMR on Stack Overflow See other posts from Stack Overflow or by ByteMR
Published on 2010-04-03T04:07:16Z Indexed on 2010/04/03 4:13 UTC
Read the original article Hit count: 336

Filed under:

I am working on a system for reserving seats. A user inputs how many seats they wish to reserve and the database will return a set of suggested seats that are not previously reserved that matches the number of seats being reserved.

For instance if I had the table:

SeatID | Reserved
-----------------
1      | false
2      | true
3      | false
4      | false
5      | false
6      | true
7      | true
8      | false
9      | false
10     | true

And the user inputs that they wish to reserve 2 seats, I would expect the query to return that seats (3, 4), (4, 5), and (8, 9) are not reserved and match the given number of input seats. Seats are organized into sections and rows. Continuous seats must be in the same row.

How would I go about structuring this query to work in such a way that it finds all available continuous seats that match the given input?

© Stack Overflow or respective owner

Related posts about sql