从其他日期时间范围(T-SQL)中减去日期时间范围
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从其他日期时间范围(T-SQL)中减去日期时间范围相关的知识,希望对你有一定的参考价值。
我需要一些帮助才能创建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
UnavailableTimes
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中的时间范围,以获得如下可用时间:
AvailableTimes
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的帮助下,我得到了开始时间和结束时间。现在我只需将它们放在一起:
;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
答案
这很好,拼图的最后一部分拼凑在一起:
;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
另一答案
我在你的开始时间有一个裂缝,你应该能够自己解决如何做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
以上是关于从其他日期时间范围(T-SQL)中减去日期时间范围的主要内容,如果未能解决你的问题,请参考以下文章
Microsoft SQL Server 2016,T-SQL:根据各个日期获取数据集的日期范围