【发布时间】: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