根据 SQL 中的工作班次查询特定日期营业时间的免费餐厅餐桌
querying free restaurant tables with hours on a specific date and according to work shifts in SQL
table结构如下:
Tables:
Id Number Title
1 1 VIP-1
2 2 VIP-2
WorkShifts:
Id DayOfWeek From To
1 Sunday 08:00 12:00
2 Sunday 17:00 20:00
3 Monday 18:00 23:00
Applications:
Id CustomerId TableId Date From To
1 1 1 2020-02-23 08:00 09:30
2 3 2 2020-02-23 10:00 11:00
请记住,最短保留时间为 1 小时。
根据以上信息如何查询特定日期的免费tables(with time)?
例如在 2020 年 2 月 23 日星期日('2020-02-23'),结果应该是:
TableId Time
1 10:00
1 11:00
1 17:00
1 18:00
1 19:00
2 08:00
2 09:00
2 11:00
2 17:00
2 18:00
2 19:00
在尝试了不同的方法后,这个存储过程终于解决了问题:
create procedure GetFreeHourByTable @Date date as
declare @startTime time(0), @endTime time(0), @day int
declare @dates table (day int, time time(0))
DECLARE cursor_shifts CURSOR
FOR SELECT
w.DayOfWeek, w.StartTime, w.EndTime
FROM
WorkingTimes w where w.DayOfWeek = DATEPART(dw, @Date) and w.IsDayOff = 0;
OPEN cursor_shifts;
FETCH NEXT FROM cursor_shifts INTO
@day,
@startTime,
@endTime;
WHILE @@FETCH_STATUS = 0
BEGIN
while @startTime < DATEADD(HOUR, -1, @endTime)
begin
set @startTime = DATEADD(HOUR, 1, @startTime)
insert into @dates values (@day, @startTime)
end
FETCH NEXT FROM cursor_shifts INTO
@day,
@startTime,
@endTime;
END;
CLOSE cursor_shifts;
DEALLOCATE cursor_shifts;
select t.*, d.* from @dates d, Tables t
where d.day = DATEPART(dw, @Date)
and not exists
(select * from Applications a
where a.tableId = t.id and a.registerDate = @Date and d.time between a.startTime and a.endTime)
所以我们可以用小时查询空闲表(按日期):
exec GetFreeHourByTable @Date = '2020-02-23';
table结构如下:
Tables:
Id Number Title
1 1 VIP-1
2 2 VIP-2
WorkShifts:
Id DayOfWeek From To
1 Sunday 08:00 12:00
2 Sunday 17:00 20:00
3 Monday 18:00 23:00
Applications:
Id CustomerId TableId Date From To
1 1 1 2020-02-23 08:00 09:30
2 3 2 2020-02-23 10:00 11:00
请记住,最短保留时间为 1 小时。 根据以上信息如何查询特定日期的免费tables(with time)? 例如在 2020 年 2 月 23 日星期日('2020-02-23'),结果应该是:
TableId Time
1 10:00
1 11:00
1 17:00
1 18:00
1 19:00
2 08:00
2 09:00
2 11:00
2 17:00
2 18:00
2 19:00
在尝试了不同的方法后,这个存储过程终于解决了问题:
create procedure GetFreeHourByTable @Date date as
declare @startTime time(0), @endTime time(0), @day int
declare @dates table (day int, time time(0))
DECLARE cursor_shifts CURSOR
FOR SELECT
w.DayOfWeek, w.StartTime, w.EndTime
FROM
WorkingTimes w where w.DayOfWeek = DATEPART(dw, @Date) and w.IsDayOff = 0;
OPEN cursor_shifts;
FETCH NEXT FROM cursor_shifts INTO
@day,
@startTime,
@endTime;
WHILE @@FETCH_STATUS = 0
BEGIN
while @startTime < DATEADD(HOUR, -1, @endTime)
begin
set @startTime = DATEADD(HOUR, 1, @startTime)
insert into @dates values (@day, @startTime)
end
FETCH NEXT FROM cursor_shifts INTO
@day,
@startTime,
@endTime;
END;
CLOSE cursor_shifts;
DEALLOCATE cursor_shifts;
select t.*, d.* from @dates d, Tables t
where d.day = DATEPART(dw, @Date)
and not exists
(select * from Applications a
where a.tableId = t.id and a.registerDate = @Date and d.time between a.startTime and a.endTime)
所以我们可以用小时查询空闲表(按日期):
exec GetFreeHourByTable @Date = '2020-02-23';