如何在SQL Server中查找重叠的日期 [英] How To find overlapping Dates in SQL Server

查看:136
本文介绍了如何在SQL Server中查找重叠的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含开始日期时间和结束日期时间的表。如何查找与其他日期重叠的微粒日期时间:
参见以下示例

 创建表#period( 
id int,
开始时间datetime,
结束时间datetime
);

插入#期值
(1,'2013-10-10 08:00:00','2013-10-10 10:00:00'),
(2,'2013-10-10 08:10:00','2013-10-10 08:20:00'),
(3,'2013-10-10 08:10:00' ,'2013-10-10 08:30:00')
(4,'2013-10-10 08:15:00','2013-10-10 08:25:00')

select * from #period


要求的输出为'2013-10-10 08:15:00','2013-10-10 08:20:00'在所有日期中都重叠。

预期输出:'2013-10-10 08:15:00''2013-10-10 08:20:00'5分钟

编辑:



我为先前的问题
道歉

 创建表#period 

网站代码varchar(20),
svrname varchar(10),
StartTime datetime,
downtimeEnd datetime

go

插入#period值
('A','S1','2013-10-10 10:00:00','2013-10-10 11:00:00'),
('A','S2',' 2013-10-10 10:00:00','2013-10-10 11:00:00'),
('A','S3','2013-10-10 10:00:00 ','2013-10-10 11:00:00'),
('A','S1','2013-10-10 03:00:00','2013-10-10 03: 30:00'),
('A','S2','2013-10-10 06:30:00','2013-10-10 07:30:00')

select * from #period

预期输出为:


 站点代码'A'
共2.5小时(以下为详细信息)
'201 3-10-10 10:00:00','2013-10-10 11:00:00'得到重叠p-1小时
'2013-10-10 03:00:00','2013-10 -10 03:30:00'-.5小时
'2013-10-10 06:30:00','2013-10-10 07:30:00'- 1小时


输出按 sitecode 分组。重叠的日期只考虑一次。
需要最终输出:


Sitecode'A'= 2.5 hours



解决方案

如果您有一组句点,并且想要它们的所有全部重叠,那么这些重叠将继续在最大开始时间和最小结束时间之间。

 选择演员表(开始时间为日期)作为日期,最大值(开始时间) ,由#periods 
组中的min(endtime)
进行强制转换(开始时间为日期);

这假设开始和结束在同一日期。第一个只是从datetime值中提取日期(这可能因数据库而异)。



min(endtime)小于<$时,没有重叠。 c $ c> max(starttime)。因此,要获取重叠的日期:

 选择cast(开始时间为日期)作为日期,max(开始时间),min( #periods 
组中的
通过强制转换(开始时间作为日期)
具有min(endtime)> max(开始时间);

编辑:



如果要重叠在网站代码中,只需将其添加到 group by中即可:

 选择网站代码,将(开始时间设为日期)作为日期, max(开始时间),min(结束时间)
,按站点代码分组,由#periods
组,cast(开始时间作为日期)
,min(结束时间)> max(开始时间);


I have a table which has startdatetime and enddatetime. how to find the the particuler datetime which is overlapping in other dates: see below example

create table #period (
    id int,
    starttime datetime,
    endtime datetime
  );

insert into #period values 
(1,'2013-10-10 08:00:00' , '2013-10-10 10:00:00'), 
(2,'2013-10-10 08:10:00' , '2013-10-10 08:20:00'), 
(3,'2013-10-10 08:10:00' , '2013-10-10 08:30:00') 
(4,'2013-10-10 08:15:00' , '2013-10-10 08:25:00') 

select * from #period


required output is '2013-10-10 08:15:00' , '2013-10-10 08:20:00' is getting overlapped in all the dates.

expected output: '2013-10-10 08:15:00' '2013-10-10 08:20:00' 5 Min

EDIT:

I apologies for previous question Here it is the proper details with example

create table #period 
(
    Sitecode varchar(20),
    svrname varchar(10),
    StartTime datetime,
    downtimeEnd datetime
) 
go  

insert into #period values 
('A','S1','2013-10-10 10:00:00' , '2013-10-10 11:00:00'), 
('A','S2','2013-10-10 10:00:00' , '2013-10-10 11:00:00'),
('A','S3','2013-10-10 10:00:00' , '2013-10-10 11:00:00'), 
('A','S1','2013-10-10 03:00:00' , '2013-10-10 03:30:00'), 
('A','S2','2013-10-10 06:30:00' , '2013-10-10 07:30:00')

select * from #period

Expected Output is:

Sitecode 'A' 
       Total 2.5 hours (below is the details)
       '2013-10-10 10:00:00' , '2013-10-10 11:00:00' getting overlapp - 1 hour
       '2013-10-10 03:00:00' , '2013-10-10 03:30:00' - .5 hour
       '2013-10-10 06:30:00' , '2013-10-10 07:30:00'- 1 hour

The output is group by sitecode. and the overlapping date considered only once. The final output is required:

Sitecode 'A' = 2.5 hours

解决方案

If you have a set of periods and want the overlaps of all of them, then the overlaps are going to be between the maximum starttime and the minimum end time.

select cast(starttime as date) as thedate, max(starttime), min(endtime)
from #periods
group by cast(starttime as date);

This assumes that the start and end are on the same date. The first just extracts the date from the datetime values (and this may vary depending on the database). The version here is for SQL Server.

You have no overlaps when min(endtime) is less than max(starttime). So, to get dates with an overlap:

select cast(starttime as date) as thedate, max(starttime), min(endtime)
from #periods
group by cast(starttime as date)
having min(endtime) > max(starttime);

EDIT:

If you want overlaps within a site code, just add that into the `group by:

select sitecode, cast(starttime as date) as thedate, max(starttime), min(endtime)
from #periods
group by sitecode, cast(starttime as date)
having min(endtime) > max(starttime);

这篇关于如何在SQL Server中查找重叠的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆