我需要一些帮助才能创建SQL查询。至少提示如何做到这一点。 我有一个带有日期时间范围的表计划表和另一个带有日期时间范围的表UnavailableTimes。从这里我想要可用的时间,从时间表中减去不可用的时间。
T-SQL(测试数据)
CREATE TABLE Schedule (
ID INT,
StartTime DATETIME,
EndTime DATETIME
)
CREATE TABLE UnavailableTimes (
ID INT,
StartTime DATETIME,
EndTime DATETIME
)
CREATE TABLE AvailableTimes (
ID INT,
StartTime DATETIME,
EndTime DATETIME
)
INSERT INTO Schedule (ID, StartTime, EndTime)
VALUES
(2, '2018-02-27 08:00', '2018-02-27 12:00'),
(2, '2018-02-27 13:00', '2018-02-27 17:00')
INSERT INTO UnavailableTimes (ID, StartTime, EndTime)
VALUES
(2, '2018-02-27 07:30', '2018-02-27 08:30'),
(2, '2018-02-27 08:00', '2018-02-27 09:30'),
(2, '2018-02-27 13:30', '2018-02-27 14:00'),
(2, '2018-02-27 16:00', '2018-02-27 18:00')
时间表
ID StartTime EndTime
2 2018-02-27 08:00 2018-02-27 12:00
2 2018-02-27 13:00 2018-02-27 17:00
不可用时间
ID StartTime EndTime
2 2018-02-27 07:30 2018-02-27 08:30
2 2018-02-27 08:00 2018-02-27 09:30
2 2018-02-27 13:30 2018-02-27 14:00
2 2018-02-27 16:00 2018-02-27 18:00
我想从Schedule中的时间范围中减去UnavailableTimes中的时间范围,以获得如下可用时间:
可用时间
ID StartTime EndTime
2 2018-02-27 09:30 2018-02-27 12:00
2 2018-02-27 13:00 2018-02-27 13:30
2 2018-02-27 14:00 2018-02-27 16:00
修改 在 MJH 的帮助下,我获得了StartTimes和EndTimes。现在我只需要把它放在一起:
;WITH S1 AS (
SELECT U.EndTime AS StartTime
FROM UnavailableTimes U
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes W
WHERE U.EndTime BETWEEN W.StartTime AND DATEADD(s, -1, W.EndTime)
)
AND EXISTS (
SELECT *
FROM Schedule S
WHERE U.EndTime BETWEEN S.StartTime AND S.EndTime
)
),
S2 AS (
SELECT S.StartTime
FROM Schedule S
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes U
WHERE S.StartTime BETWEEN U.StartTime AND U.EndTime
)
)
SELECT StartTime
FROM S1
UNION ALL
SELECT StartTime
FROM S2
ORDER BY 1
;WITH S1 AS (
SELECT U.StartTime AS EndTime
FROM UnavailableTimes U
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes W
WHERE U.StartTime BETWEEN DATEADD(s, 1, W.StartTime) AND W.EndTime
)
AND EXISTS (
SELECT *
FROM Schedule S
WHERE U.StartTime BETWEEN S.StartTime AND S.EndTime
)
),
S2 AS (
SELECT S.EndTime
FROM Schedule S
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes U
WHERE S.EndTime BETWEEN U.StartTime AND U.EndTime
)
)
SELECT EndTime
FROM S1
UNION ALL
SELECT EndTime
FROM S2
ORDER BY 1
答案 0 :(得分:1)
我在你的开始时间有一个裂缝,你应该能够自己解决如何做EndTimes:
;WITH S1 AS (
SELECT U.EndTime AS StartTime
FROM UnavailableTimes U
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes W
WHERE U.EndTime BETWEEN W.StartTime AND DATEADD(s, -1, W.EndTime)
)
AND EXISTS (
SELECT *
FROM Schedule S
WHERE U.EndTime BETWEEN S.StartTime AND S.EndTime
)
),
S2 AS (
SELECT S.StartTime
FROM Schedule S
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes U
WHERE S.StartTime BETWEEN U.StartTime AND U.EndTime
)
)
SELECT StartTime
FROM S1
UNION ALL
SELECT StartTime
FROM S2
ORDER BY 1
答案 1 :(得分:0)
这很好,拼图的最后一部分拼凑在一起:
;WITH S1 AS (
SELECT U.EndTime AS StartTime
FROM UnavailableTimes U
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes W
WHERE U.EndTime BETWEEN W.StartTime AND DATEADD(s, -1, W.EndTime)
)
AND EXISTS (
SELECT *
FROM Schedule S
WHERE U.EndTime BETWEEN S.StartTime AND S.EndTime
)
),
S2 AS (
SELECT S.StartTime
FROM Schedule S
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes U
WHERE S.StartTime BETWEEN U.StartTime AND U.EndTime
)
),
S3 AS (
SELECT StartTime
FROM S1
UNION ALL
SELECT StartTime
FROM S2
),
S AS (
SELECT StartTime, ROW_NUMBER() OVER(ORDER BY StartTime) RN
FROM S3
),
E1 AS (
SELECT U.StartTime AS EndTime
FROM UnavailableTimes U
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes W
WHERE U.StartTime BETWEEN DATEADD(s, 1, W.StartTime) AND W.EndTime
)
AND EXISTS (
SELECT *
FROM Schedule S
WHERE U.StartTime BETWEEN S.StartTime AND S.EndTime
)
),
E2 AS (
SELECT S.EndTime
FROM Schedule S
WHERE NOT EXISTS (
SELECT *
FROM UnavailableTimes U
WHERE S.EndTime BETWEEN U.StartTime AND U.EndTime
)
),
E3 AS (
SELECT EndTime
FROM E1
UNION ALL
SELECT EndTime
FROM E2
),
E AS (
SELECT EndTime, ROW_NUMBER() OVER(ORDER BY EndTime) RN
FROM E3
)
SELECT S.StartTime, E.EndTime
FROM S INNER JOIN E
ON S.RN = E.RN
ORDER BY S.RN