Overlaps II
Start Stop Machine Prodid Overlap
2017-09-11 05:30:16.000 2017-09-11 10:41:40.000 CNC M 10 17-012424 281
2017-09-11 06:00:42.000 2017-09-11 06:38:56.000 CNC M 9 17-012793 36
2017-09-11 06:02:52.000 2017-09-11 13:03:39.000 CNC M 1 17-012808 415
2017-09-11 06:08:39.000 2017-09-11 13:55:19.000 CNC M 11 17-012512 439
2017-09-11 06:36:37.000 2017-09-11 07:41:35.000 CNC M 6 17-012811 57
嗨基于Sarslan's answer,我解决了我的问题。在我之前的例子中,我们只关注下一个估计重叠的条目。
是否有可能扩展SQL代码,以便在并行运行时查看所有条目?
例如:第一个条目一直持续到10:41
。它与接下来的5个条目并行运行。所以重叠应该更多。
以下是上一个示例中的代码。
DECLARE @Tbl TABLE(Start DATETIME, [Stop] DATETIME,Machine VARCHAR(20),Prodid VARCHAR(20))
INSERT INTO @Tbl VALUES
('2017-09-11 06:36:37.000','2017-09-11 07:41:35.000','CNC M 6','17-012811'),
('2017-09-11 07:49:27.000','2017-09-11 10:10:01.000','CNC M 6','17-012811'),
('2017-09-11 09:34:46.000','2017-09-11 10:10:01.000','CNC M 7','17-012738'),
('2017-09-11 10:14:50.000','2017-09-11 11:50:15.000','CNC M 6','17-012811'),
('2017-09-11 10:15:50.000','2017-09-11 11:50:15.000','CNC M 7','17-012738'),
('2017-09-11 11:55:52.000','2017-09-11 12:17:03.000','CNC M 6','17-012811'),
('2017-09-11 11:56:15.000','2017-09-11 12:17:14.000','CNC M 7','17-012738')
SELECT T1.*, DATEDIFF(MINUTE, Ovr.Start , T1.[Stop]) Overlapping FROM @Tbl T1
OUTER APPLY (SELECT TOP 1 * FROM @Tbl T2 WHERE T2.Start > T1.Start AND T2.Start < T1.Stop ORDER BY T2.Start) ) AS Ovr
答案 0 :(得分:0)
迈克尔,
如果第1行与第2行重叠1小时, 并且还将第3行重叠2小时。
您希望看到什么价值重叠&#39;在第1行?
您是否需要了解其他行如何重叠?
由于
答案 1 :(得分:0)
迈克尔,
我没有对此提出任何效率声明,但粗略地说,这可能会做你想要的。
我没有2016年,所以你可能会在那里得到一些我无法想到的新巫术。
看看它是怎么回事......
SELECT TOP (100) PERCENT
A.Start, A.Stop, A.Machine, A.Prodid
, SUM(DATEDIFF(n,
CASE WHEN B.Start > A.Start THEN B.Start ELSE A.Start END,
CASE WHEN A.Stop < B.Stop THEN A.Stop ELSE B.Stop END)
) AS overlap
, COUNT(B.Start) AS [#Overlaps]
FROM
test.tbl AS A LEFT OUTER JOIN
test.tbl AS B ON B.Start < A.Stop AND B.Stop > A.Start AND (A.Start < B.Start OR B.Start IS NULL)
GROUP BY A.Start, A.Stop, A.Machine, A.Prodid
ORDER BY A.Start