TSQL从日期时间范围获得重叠时段

时间:2013-04-08 07:02:23

标签: tsql datetime overlapping

我有一个日期范围的表格,我需要它们行之间的重叠周期(以小时为单位)的总和。

这是一个架构示例:

create table period (
    id int,
    starttime datetime,
    endtime datetime,
    type varchar(64)
  );

insert into period values (1,'2013-04-07 8:00','2013-04-07 13:00','Work');
insert into period values (2,'2013-04-07 14:00','2013-04-07 17:00','Work');
insert into period values (3,'2013-04-08 8:00','2013-04-08 13:00','Work');
insert into period values (4,'2013-04-08 14:00','2013-04-08 17:00','Work');
insert into period values (5,'2013-04-07 10:00','2013-04-07 11:00','Holyday'); /* 1h overlapping with 1*/
insert into period values (6,'2013-04-08 10:00','2013-04-08 20:00','Transfer'); /* 6h overlapping with 3 and 4*/
insert into period values (7,'2013-04-08 11:00','2013-04-08 12:00','Test');  /* 1h overlapping with 3 and 6*/

它的小提琴:http://sqlfiddle.com/#!6/9ca31/10

我预计会有8小时的重叠时间: 1小时(id 1超过id 1) 6h(id 3和4的id 6) 1小时(身份3和6的身份7)

我检查一下:select overlapping datetime events with SQL但似乎没有做我需要的。

谢谢。

2 个答案:

答案 0 :(得分:2)

select sum(datediff(hh, case when t2.starttime > t1.starttime then t2.starttime else t1.starttime end,
    case when t2.endtime > t1.endtime then t1.endtime else t2.endtime end))
from period t1 
join period t2 on t1.id < t2.id
where t2.endtime > t1.starttime and t2.starttime < t1.endtime;

更新以处理多个重叠:

select sum(datediff(hh, start, fin))
from (select distinct
case when t2.starttime > t1.starttime then t2.starttime else t1.starttime end as start,
case when t2.endtime > t1.endtime then t1.endtime else t2.endtime end as fin
from period t1 
join period t2 on t1.id < t2.id
where t2.endtime > t1.starttime and t2.starttime < t1.endtime
) as overlaps;

答案 1 :(得分:0)

我有一些&#34;脏&#34;解。希望这会有所帮助:)

with src as (
    select
     convert(varchar, starttime, 112) [start_date]
    , cast(left(convert(varchar, starttime, 108), 2) as int) [start_time]
    , convert(varchar, endtime, 112) [end_date]
    , cast(left(convert(varchar, endtime, 108), 2) as int) [end_time]
    , id
    from [period]),

[gr] as (
    select
    row_number() over(order by s1.[start_date], s1.[start_time], s1.[end_time], s2.[start_time], s2.[end_time]) [no]
    , s1.[start_date] [date]
    , s1.[start_time] [t1]
    , s1.[end_time] [t2]
    , s2.[start_time] [t3]
    , s2.[end_time] [t4]
    from src s1
    join src s2 on s1.[start_date] = s2.[start_date]
        and s1.[end_date] = s2.[end_date]
        and (s1.[start_time] between s2.[start_time] and s2.[end_time] or s1.[end_time] between s2.[start_time] and s2.[end_time])
        and s1.id != s2.id),

[raw] as (
    select [no], [date], [t1] [h] from [gr] union all
    select [no], [date], [t2] from [gr] union all
    select [no], [date], [t3] from [gr] union all
    select [no], [date], [t4] from [gr]),

[max_min] as (
    select [no], [date], max(h) [max_h], min(h) [min_h]
    from [raw]
    group by [no], [date]
),

[result] as (
    select [raw].*
    from [raw]
    left join [max_min] on [raw].[no] = [max_min].[no]
        and ([raw].h = [max_min].[max_h] or [raw].h = [max_min].[min_h])
    where [max_min].[no] is null),

[final] as (
    select distinct r1.[date], r1.h [start_h], r2.h [end_h], abs(r1.h - r2.h) [dif]
    from [result] r1
    join [result] r2 on r1.[no] = r2.[no]
    where abs(r1.h - r2.h) > 0
    and r1.h > r2.h)

select sum(dif) [overlapping hours] from [final]

SQLFiddle