获取不在数据库中的范围

时间:2014-10-23 15:52:46

标签: sql sql-server database

我希望有时候每个房间都没有举办活动。当天的开头是9:00:00,结尾是22:00:00

我的数据库是这样的:

Event       EventStart  EventEnd    Days                Rooms   DayStarts
CISC 3660   09:00:00    12:30:00    Monday              7-3     9/19/2014   
MATH 2501   15:00:00    17:00:00    Monday:Wednesday    7-2     10/13/2014  
CISC 1110   14:00:00    16:00:00    Monday              7-3     9/19/2014   

我希望得到数据库中没有的时间。

离。对于SelectedDate(2014年9月19日),该表应返回:

Room  FreeTimeStart  FreeTimeEnd
7-3   12:30:00       14:00:00
7-3   16:00:00       22:00:00

EX2。 SelectedDate(10/13/2014):

Room  FreeTimeStart  FreeTimeEnd
7-2    9:00:00       15:00:00
7-2   17:00:00       22:00:00  

我尝试的是这样的:

select * from Events where ________ NOT BETWEEN eventstart AND eventend;

但我不知道该放置什么空间。

2 个答案:

答案 0 :(得分:1)

这是一个非常复杂的请求。 SQL最适合使用集合,而不是逐行查看。这就是我想出的。为了更容易理解,我把它写成了一系列CTE,所以我可以一步一步解决问题。我并不是说这是最好的方法,但它不需要使用任何游标。您需要事件表和房间名称表(否则,您看不到没有预订的房间)。

以下是查询,我将解释该方法。

DECLARE @Events TABLE (Event varchar(20), EventStart Time, EventEnd Time, Days varchar(50), Rooms varchar(10), DayStarts date)

INSERT INTO @Events
SELECT 'CISC 3660',   '09:00:00',    '12:30:00',    'Monday',              '7-3',     '9/19/2014' UNION
SELECT 'MATH 2501',   '15:00:00',    '17:00:00',    'Monday:Wednesday',    '7-2',     '10/13/2014' UNION
SELECT 'CISC 1110',   '14:00:00',    '16:00:00',    'Monday',              '7-3',     '9/19/2014' 

DECLARE @Rooms TABLE (RoomName varchar(10))
INSERT INTO @Rooms
SELECT '7-2' UNION 
SELECT '7-3'

DECLARE @SelectedDate date = '9/19/2014'
DECLARE @MinTimeInterval int = 30 --smallest time unit room can be reserved for
;WITH
  D1(N) AS (
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
           ),
  D2(N) AS (SELECT 1 FROM D1 a, D1 b),
  D4(N) AS (SELECT 1 FROM D2 a, D2 b),
  Numbers AS (SELECT TOP 3600 ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) -1 AS Number FROM D4),
  AllTimes AS 
    (SELECT CAST(DATEADD(n,Numbers.Number*@MinTimeInterval,'09:00:00') as time) AS m FROM Numbers
    WHERE DATEADD(n,Numbers.Number*@MinTimeInterval,'09:00:00') <= '22:00:00'),
  OccupiedTimes AS (
    SELECT e.Rooms, ValidTimes.m
    FROM @Events E
    CROSS APPLY (SELECT m FROM AllTimes WHERE m BETWEEN CASE WHEN e.EventStart = '09:00:00' THEN e.EventStart ELSE DATEADD(n,1,e.EventStart) END and CASE WHEN e.EventEnd = '22:00:00' THEN e.EventEnd ELSE DATEADD(n,-1,e.EventEnd) END) ValidTimes
    WHERE e.DayStarts = @SelectedDate
    ),
    AllRoomsAllTimes AS (
        SELECT * FROM @Rooms R CROSS JOIN AllTimes
    ), AllOpenTimes AS (
    SELECT a.*, ROW_NUMBER() OVER( PARTITION BY (a.RoomName) ORDER BY a.m) AS pos
    FROM AllRoomsAllTimes A
    LEFT OUTER JOIN OccupiedTimes o ON a.RoomName = o.Rooms AND a.m = o.m
    WHERE o.m IS NULL
    ), Finalize AS (
    SELECT a1.RoomName,
        CASE WHEN a3.m IS NULL OR  DATEDIFF(n,a3.m, a1.m) > @MinTimeInterval THEN a1.m else NULL END AS FreeTimeStart,
        CASE WHEN a2.m IS NULL OR DATEDIFF(n,a1.m,a2.m) > @MinTimeInterval THEN A1.m ELSE NULL END AS FreeTimeEnd,
        ROW_NUMBER() OVER( ORDER BY a1.RoomName )  AS Pos
    FROM AllOpenTimes A1
    LEFT OUTER JOIN AllOpenTimes A2 ON a1.RoomName = a2.RoomName and a1.pos = a2.pos-1
    LEFT OUTER JOIN AllOpenTimes A3 ON a1.RoomName = a3.RoomName and a1.pos = a3.pos+1
    WHERE A2.m IS NULL OR DATEDIFF(n,a1.m,a2.m) > @MinTimeInterval
    OR
    A3.m IS NULL OR DATEDIFF(n,a3.m, a1.m) > @MinTimeInterval
    )
    SELECT F1.RoomName, f1.FreeTimeStart, f2.FreeTimeEnd FROM Finalize F1
    LEFT OUTER JOIN Finalize F2 ON F1.Pos = F2.pos-1 AND f1.RoomName = f2.RoomName
    WHERE f1.pos % 2 = 1

在前几行中,我创建了临时变量来模拟你的表事件和房间。 变量@MinTimeInterval确定房间安排的时间间隔(每30分钟,15分钟等 - 这个数字需要均匀分配到60)。

由于SQL无法查询丢失的数据,因此我们需要创建一个包含我们要检查的所有时间的表。 WITH中的前几行创建了一个名为AllTimes的表,它是当天可能的所有时间间隔。

接下来,我们得到一个被占用的所有时间的列表(OccupiedTimes),然后LEFT OUTER JOIN这个表到AllTimes表,它给了我们所有可用的时间。由于我们只想要每个空闲时间的开始和结束,因此创建Finalize表,该表将每个记录连接到表中的上一个和下一个记录。如果这些行中的时间大于@MinTimeInterval,那么我们知道它是空闲时间的开始或结束。

最后,我们自己加入最后一个表,将开始和结束时间放在同一行,只查看每一行。

如果事件中的单行跨越多天或多个房间,则需要进行调整。

答案 1 :(得分:1)

这是一个将返回完整图片的解决方案&#34;包括当天未预订的客房:

Declare @Date char(8) = '20141013'
;
WITH cte as
(
    SELECT *
    FROM -- use your table name instead of the VALUES construct
    (VALUES
    ('09:00:00','12:30:00' ,'7-3', '20140919'),
    ('15:00:00','17:00:00' ,'7-2', '20141013'),
    ('14:00:00','16:00:00' ,'7-3', '20140919')) x(EventStart , EventEnd,Rooms, DayStarts)
), cte_Days_Rooms AS
-- get a cartesian product for the day specified and all rooms as well as the start and end time to compare against
(
    SELECT y.EventStart,y.EventEnd, x.rooms,a.DayStarts FROM 
    (SELECT @Date DayStarts) a
    CROSS JOIN
    (SELECT DISTINCT Rooms FROM cte)x
    CROSS JOIN
    (SELECT '09:00:00' EventStart,'09:00:00' EventEnd UNION ALL
     SELECT '22:00:00' EventStart,'22:00:00' EventEnd) y        
), cte_1 AS
-- Merge the original data an the "base data"
(
    SELECT * FROM cte WHERE DayStarts=@Date
    UNION ALL
    SELECT * FROM cte_Days_Rooms
), cte_2 as
-- use the ROW_NUMBER() approach to sort the data
(
    SELECT *, ROW_NUMBER() OVER(PARTITION BY DayStarts, Rooms ORDER BY EventStart) as pos
    FROM cte_1
)
-- final query: self join with an offest of one row, eliminating duplicate rows if a room is booked starting 9:00 or ending 22:00
SELECT c2a.DayStarts, c2a.Rooms , c2a.EventEnd, c2b.EventStart 
FROM cte_2 c2a
INNER JOIN cte_2 c2b on c2a.DayStarts = c2b.DayStarts AND c2a.Rooms =c2b.Rooms AND c2a.pos = c2b.pos -1
WHERE c2a.EventEnd <> c2b.EventStart
ORDER BY c2a.DayStarts, c2a.Rooms