获取T-SQL中未保留的位置

时间:2018-08-08 22:21:22

标签: sql-server tsql

我有我的位置(eventos_espacios)的表,其中有以下列:

idPlace, name, minCapacity, maxCapacity, and cleaningTime

我有预订表(eventos_evento_reservacion),在这里:

idReservation, idPlace, startDate, endDate, and wantedCapacity

这是我的示例数据:

DECLARE @eventos_espacios TABLE (idPlace INT, name varchar(20), minCapacity INT, maxCapacity INT, cleaningTime INT)
INSERT INTO @eventos_espacios
SELECT 1, 'Conference Room', 20, 50, 120

SELECT *
    FROM @eventos_espacios

DECLARE @reservations  TABLE (idReservation INT, idPlace INT, startDate datetime, endDate datetime, wantedCapacity INT)
INSERT INTO @reservations
select 1, 1, '2018-08-07 18:00','2018-08-07 21:00',45

SELECT *
    FROM
    @reservations

F / E: 我有一个活动,从今天开始15到17:30。在id = 1的地方,清洁时间为120分钟。 我真的很想从@eventos_espacios获取所有未在@reservations中保留的可用位置,并且我可以使用15到19:30 hrs,同时还要考虑已保留的位置不包括cleaningTime。 我有这样的事情,但似乎没有考虑某些情况,我不知道为什么。

SELECT DISTINCT
ee.idPlace,
ee.name
FROM @reservations eer
INNER JOIN @eventos_espacios ee on ee.idPlace = eer.idPlace
WHERE 30 between ee.minCapacity and ee.maxCapacity
AND ee.idPlace NOT IN
(
    SELECT DISTINCT
    ee.idPlace
    FROM @reservations eer
    INNER JOIN @eventos_espacios ee on ee.idPlace = eer.idPlace    
    WHERE ( 
                ( '2018-08-07 15:00' >= startDate AND '2018-08-07 17:30' <= DATEADD(MINUTE,ee.cleaningTime,endDate)) or 
                ( '2018-08-07 17:30' > startDate AND '2018-08-07 17:30' < DATEADD(MINUTE,ee.cleaningTime,endDate)) or 
                ( '2018-08-07 15:00' > startDate AND '2018-08-07 15:00' < DATEADD(MINUTE,ee.cleaningTime,endDate)) or 
                ( '2018-08-07 15:00' < startDate AND '2018-08-07 17:30' > DATEADD(MINUTE,ee.cleaningTime,endDate)) or 
                ( startDate >= '2018-08-07 15:00' AND DATEADD(MINUTE,ee.cleaningTime,endDate) <= '2018-08-07 17:30') 
          )
)

这不应返回下一个信息:

1   Conference Room 20  50  120

由于120分钟的清洁时间已添加到``2018-08-07 17:30'',因此该地方将免费开放至``2018-08-07 19:30'',并且已经有一个预订时间为``2018-08- 07 18:00'至'2018-08-07 21:00'。

从'2018-08-07 21:00'到'2018-08-07 22:00'的事件也应该发生相同的情况,因为idPlace = 1的预订应考虑cleaningTime且应免费使用直到23小时。

谢谢。

1 个答案:

答案 0 :(得分:0)

在此刻刺死

declare @start datetime = '2018-08-07 15:00'
declare @end datetime = '2018-08-07 17:30'
declare @capacity_needed int = 30

DECLARE @eventos_espacios TABLE (idPlace INT, name varchar(20), minCapacity INT, maxCapacity INT, cleaningTime INT)
INSERT INTO @eventos_espacios
SELECT 1, 'Conference Room 1', 20, 50, 120
union
SELECT 2, 'Conference Room 2', 20, 50, 15
union 
SELECT 3, 'Conference Room 3', 20, 50, 30
union 
SELECT 4, 'Conference Room 4', 20, 150, 180
union 
SELECT 5, 'Conference Room 5', 5, 20, 10

SELECT *
FROM @eventos_espacios

DECLARE @reservations  TABLE (idReservation INT, idPlace INT, startDate datetime, endDate datetime, wantedCapacity INT)
INSERT INTO @reservations
select 1, 1, '2018-08-07 18:00','2018-08-07 21:00',45
union all
select 1, 2, '2018-08-07 18:00','2018-08-07 21:00',30
union all
select 1, 3, '2018-08-07 18:00','2018-08-07 21:00',30
union all
select 1, 5, '2018-08-07 18:00','2018-08-07 21:00',15

SELECT *
FROM
@reservations

select e.*
from @eventos_espacios e left join @reservations r on r.idPlace = e.idPlace
where (r.startDate >= DATEADD(minute, e.cleaningtime,  @end)
or r.idReservation is null) and @capacity_needed between e.minCapacity and e.maxCapacity