For a reservation system there is an inventory table and each item has a quantity (e.g. there are 20 chairs). Now the user can make a reservation for a specific period (e.g. 5 chairs for two hours "2010-11-23 15:00" - "2010-11-23 17:00"; another reservation could be for several days "2010-11-24 11:00" - "2010-11-26 14:00").
What's the best way to check, how many items are still available for the requested period?
The user should enter the time he wants to make a reservation (from, until) and he should see how many inventory items are still available for this period.
table "inventory"
-------------------
inventory_id (int)
quantity (int)
table "reservation"
-------------------
reservation_id (int)
inventory_id (int)
quantity (int)
from (datetime)
until (datetime)
The reservations can be overlapping, but for a point in time, only inventory.quantity items should be reserved.
Simple Example:
We have 40 chairs.
The following reservations exist:
R1 2010-11-23 14:00 - 2010-11-23 15:30 -> 5 chairs reserved
R2 2010-11-23 15:00 - 2010-11-23 16:00 -> 10 chairs reserved
R3 2010-11-23 17:00 - 2010-11-23 17:30 -> 20 chairs reserved
A user makes several reservation requests (queries):
Q1 2010-11-23 15:00 - 2010-11-23 17:00 -> 25 chairs are available
Q2 2010-11-23 15:45 - 2010-11-23 17:00 -> 30 chairs are available
Q3 2010-11-23 16:30 - 2010-11-23 18:00 -> 30 chairs are available
Q4 2010-11-23 15:10 - 2010-11-23 15:20 -> 25 chairs are available
Q5 2010-11-23 13:30 - 2010-11-23 17:30 -> 20 chairs are available
How would I query the maximum available quantity for a requested period? Or is a different table design needed? The target database systems are Oracle and SQL-Server.
Update:
I tried to "visualize" the reservations R1 and R2 and the queries Q1 - Q5 without changing the original examples. I added Q4 and Q5 as additional examples. av shows the available count.
R1 R2 R3 av
13:30 40 Q5
14:00 5 35 Q5
14:30 5 35 Q5
15:00 5 10 25 Q1 Q5
15:10 5 10 25 Q1 Q4 Q5
15:20 5 10 25 Q1 Q5
15:30 10 30 Q1 Q5
15:45 10 30 Q1 Q2 Q5
16:00 40 Q1 Q2 Q5
16:30 40 Q1 Q2 Q3 Q5
17:00 20 20 Q3 Q5
av 25 30 20 25 20