用于查询特定时间段(预订系统)的最大可用项数的SQL

时间:2010-08-08 09:05:13

标签: sql sql-server oracle

对于预订系统,有一个库存表,每个项目都有数量(例如,有20把椅子)。现在,用户可以在特定时间段内进行预订(例如,5个椅子,两个小时“2010-11-23 15:00” - “2010-11-23 17:00”;另一个预订可以连续几天“2010-11-24 11:00” - “2010-11-26 14:00”)。

最佳检查方法是什么,请求期间仍有多少项目可供使用?

用户应该输入他想要预订的时间(从,直到),他应该看到这段时间内仍有多少库存商品可用。

table "inventory"
-------------------
inventory_id (int) 
quantity (int)

table "reservation"
-------------------
reservation_id (int)
inventory_id (int)
quantity (int)
from (datetime)
until (datetime)

预订可能会重叠,但对于某个时间点,只应保留 inventory.quantity 项目。

简单示例:

我们有40把椅子。

存在以下保留:

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

用户发出多个预订请求(查询):

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

如何查询请求期间的最大可用数量?或者需要不同的桌面设计?目标数据库系统是Oracle和SQL-Server。

更新

我试图“可视化”保留R1和R2以及查询Q1-Q5而不更改原始示例。我添加了Q4和Q5作为附加示例。 av显示可用的数量。

       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

2 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情(完整的工作示例)

DECLARE @inventory TABLE(
    inventory_id int, 
    quantity int
)

DECLARE @reservation TABLE(
    reservation_id int,
    inventory_id int,
    quantity int,
    [from] datetime,
    until datetime
)

INSERT INTO @inventory SELECT 1, 40

INSERT INTO @reservation SELECT 1, 1, 5, '2010-11-23 14:00 ', '2010-11-23 15:30'
INSERT INTO @reservation SELECT 1, 1, 10, '2010-11-23 15:00 ', '2010-11-23 16:00'

DECLARE @Start DATETIME,
        @End DATETIME

SELECT  @Start = '2010-11-23 15:00',
        @End = '2010-11-23 17:00'

SELECT  TotalUsed.inventory_id,
        i.quantity - ISNULL(TotalUsed.TotalUsed,0) Available
FROM    @inventory i LEFT JOIN
        (
            SELECT  inventory_id,
                    SUM(quantity) TotalUsed
            FROM    @reservation
            WHERE   [from] BETWEEN @Start AND @End
            OR      until BETWEEN @Start AND @End
            GROUP BY inventory_id
        ) TotalUsed ON  TotalUsed.inventory_id = i.inventory_id


SELECT  @Start = '2010-11-23 15:45',
        @End = '2010-11-23 17:00'

SELECT  TotalUsed.inventory_id,
        i.quantity - ISNULL(TotalUsed.TotalUsed,0) Available
FROM    @inventory i LEFT JOIN
        (
            SELECT  inventory_id,
                    SUM(quantity) TotalUsed
            FROM    @reservation
            WHERE   [from] BETWEEN @Start AND @End
            OR      until BETWEEN @Start AND @End
            GROUP BY inventory_id
        ) TotalUsed ON  TotalUsed.inventory_id = i.inventory_id

<强>结果

inventory_id Available
------------ -----------
1            25


inventory_id Available
------------ -----------
1            30

答案 1 :(得分:1)

使用SQLServer语法:

SELECT i.inventory_id,
       MAX(i.quantity) - COALESCE(SUM(r.quantity), 0) AS available            
FROM INVENTORY i     
LEFT JOIN RESERVATIONS r 
ON (r.inventory_id = i.inventory_id AND
    r.[from] <= @End AND
    r.until >= @Start)           
GROUP BY i.inventory_id

我假设提供的结构是所使用的实际结构的简化版本 - 如果没有,我建议不要使用FROM等关键字作为列名。

编辑:新查询,假设预订仅到最近的分钟,且不超过一周:

with number_cte(n, n2) as 
 (select n, n+1 n2 from (select 0 n) m union all select n+1 n, n2+1 n2 
  from number_cte where n < datediff("mi",@start,@end))
SELECT i.inventory_id, max(i.quantity) - COALESCE(max(a.alloc), 0) AS available 
from INVENTORY as i  
join
(select n.datesel, r.inventory_id, sum(r.quantity) alloc from
 (select dateadd("mi",n,@Start) datesel from number_cte) as n  
 JOIN RESERVATIONS r 
 ON n.datesel between r.[from] AND r.until 
 GROUP BY n.datesel, r.inventory_id) a 
on i.inventory_id = a.inventory_id
GROUP BY i.inventory_id option (maxrecursion 10080)

这在Oracle中实际上会更容易,因为您可以使用逐级连接而不是CTE - 如果您要保留超过一周的保留,则需要相应地增加maxrecursion数。