【问题标题】:Calculating number of bookings per slot in booking system计算预订系统中每个时段的预订数量
【发布时间】:2020-10-17 02:42:39
【问题描述】:

我正在尝试创建一个预订系统,允许客户从可用位置列表中选择一个。时段是已知的(09:00 - 17:00,每 15 分钟一班)。

预订时长取决于要收集的物品数量(在其他地方计算),并且总是四舍五入到最近的 15 分钟时间段。

我要做的是查询现有预订并返回一个插槽列表和每个插槽中的预订数量。如果预订在某个时间段内(例如,09:00 - 10:00 的预订将在 09:00、09:15、09:30 和 09:45),我只想在每个时间段中添加 1这些插槽的计数。

然后,我将把这个结果与员工列表结合起来,以显示有多少员工可用(以及空位是否空闲),但这不是这个问题的一部分。

返回数据示例:

TimeSlot | BookingsCount
------------------------
09:00    | 2
09:15    | 2
09:30    | 1
09:45    | 1
10:00    | 0
create table ScheduleTimes (t time);
insert into ScheduleTimes (t) values(('09:00'));
insert into ScheduleTimes (t) values(('09:15'));
insert into ScheduleTimes (t) values(('09:30'));
insert into ScheduleTimes (t) values(('09:45'));
insert into ScheduleTimes (t) values(('10:00'));
insert into ScheduleTimes (t) values(('10:15'));
insert into ScheduleTimes (t) values(('10:30'));
insert into ScheduleTimes (t) values(('10:45'));
insert into ScheduleTimes (t) values(('11:00'));
insert into ScheduleTimes (t) values(('11:15'));
insert into ScheduleTimes (t) values(('11:30'));
insert into ScheduleTimes (t) values(('11:45'));
insert into ScheduleTimes (t) values(('12:00'));
insert into ScheduleTimes (t) values(('12:15'));
insert into ScheduleTimes (t) values(('12:30'));

create table CollectionSchedule (ScheduleID int, CustomerID int, CollectionStart time, CollectionEnd time);
insert into CollectionSchedule values (1, 111, '09:00', '10:00');
insert into CollectionSchedule values (2, 222, '09:00', '09:30');
insert into CollectionSchedule values (3, 333, '09:30', '10:00');
insert into CollectionSchedule values (4, 444, '10:00', '10:15');
insert into CollectionSchedule values (5, 555, '10:00', '11:00');
insert into CollectionSchedule values (6, 666, '10:15', '10:45');
insert into CollectionSchedule values (7, 777, '10:45', '11:00');

I've created an SQL Fiddle which is available here which has a demo schema set up. 架构不是一成不变的,可以根据需要进行更改,这正是我目前所想到的。

这是演示架构:

收集时间表(已预约的清单):

create table CollectionSchedule (ScheduleID int, CustomerID int, CollectionStart time, CollectionEnd time);

ScheduleTimes(可用时隙列表);

create table ScheduleTimes (t time);

这是我最近的查询尝试:

select 
    st.t,
    SUM(sq1.intimeframe) as BookingsCount
from
    ScheduleTimes st
    left join (
        select 
            st.t as Slot,
            case when (st.t <= cs.CollectionStart and st.t < cs.CollectionEnd) then 1 else 0 end as intimeframe 
        from CollectionSchedule cs, @ScheduleTimes st
    ) sq1 on st.t = sq1.Slot
group by st.t

但这会为每个插槽中的预订数量返回不正确的值,因为它会多次计算条目,而且我显然走错了路并且有点迷路。任何帮助将不胜感激。

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    这样的事情会返回适当的值吗?

    SELECT Agg.T, COUNT(*) 
    FROM(SELECT s.T FROM CollectionSchedule as c
    INNER JOIN ScheduleTimes as s 
    ON s.T >= c.CollectionStart AND s.T < c.CollectionEnd) as Agg
    GROUP BY Agg.T;
    

    SQL Fiddle

    【讨论】:

    • 我的朋友,你做到了。感谢您能够破译一个问题的运球混乱,并教我新事物。是时候开始学习交叉连接了。再次感谢。
    • 严格来说这不是CROSS JOIN。将其更改为 INNER 并将 WHERE 子句设置为 ON 子句。
    • @gvee 有道理。如果将来有人有相同类型的问题,我会编辑答案
    【解决方案2】:

    只是在区间交点的左连接

    select 
        st.t,
        count(cs.ScheduleID) n
    from
        ScheduleTimes st
        left join CollectionSchedule cs on cs.CollectionStart < dateadd(minute, 15, st.t) and  st.t < cs.CollectionEnd
    group by st.t
    

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 1970-01-01
      • 2010-10-21
      • 2018-08-18
      相关资源
      最近更新 更多