Sirs,
I have a ticketing system. Now I have to select adjacent places when the user asks for 2 or 3 tickets.
Every ticket has a line and column number. The concept of adjacent places is places in the same line with adjacent columns numbers.
These tickets are in a sql server database. Any ideas about this algorithm to search for available adjacent seats?
Regards,
Camilo
You can get two adjacent seats by joining the table aginst itself on Column = Column+1:
You can extend this to 3-4 seat chains by joining repeatedly on Column=Column+1, +2, +3. If you want a more generic solution for any sequence length you’re going to have to use recursive CTEs and it gets complicated. For most use cases the simple join will work fine.
For example:
The recursive query will return all available seat sequences in a set that contains the starting seat number and the length of the sequnce. If you want only sequences of length 3, you add the necessary WHERE clause and the query will return the seat (1,4) that is the only one with 2 more available seats next to it in my sample data.