在SQL Server中查找重叠日期范围并调整重叠记录结束时间
在SQL Server中处理时间重叠记录并调整结束时间
需求:查找存在时间重叠的记录,按照示例调整其结束时间,同时希望结果中包含所有合并的ID。
测试数据
declare @t table(id int identity(1,1), name varchar(20), startdatetime datetime, enddatetime datetime); insert into @t(name, startdatetime, enddatetime) values ('ABC', '20200102 08:30', '20200102 09:30'), ('ABC', '20200102 08:31', '20200102 10:30'), ('ABC', '20200102 04:40', '20200102 05:30'), ('ABC', '20200102 04:55', '20200102 07:30'), ('XYZ', '20200102 04:40', '20200102 05:30'), ('XYZ', '20200102 04:40', '20200102 05:30'), ('XYZ', '20200102 05:20', '20200102 06:30'), -- ('x', '20200102 02:40', '20200102 03:30'), ('x', '20200102 03:30', '20200102 04:55'), ('x', '20200102 04:20', '20200102 05:35'), ('x', '20200102 05:35', '20200102 06:42'), ('x', '20200102 06:00', '20200102 08:15');
期望结果
id name startdatetime enddatetime 1 ABC 20200102 08:30 20200102 08:30 2 ABC 20200102 08:31 20200102 10:30 3 ABC 20200102 04:40 20200102 04:54 4 ABC 20200102 04:55 20200102 07:30
当前问题
已尝试部分查询,但仅能识别重叠记录,无法得到符合上述要求的结果。
内容的提问来源于stack exchange,提问作者Shawn
相关产品推荐
相关产品推荐

