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?
Using SQL Server 2005/2008:
Note that this returns IDs 3, 4, and 8, since there are two free seats starting at each of those positions.
This also assumes that Seat IDs are always sequential. If not, you can get into
ROW_NUMBER().