1

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

这是一个模式示例:

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

我预计 8h 重叠小时的总和:1h(id 5 over id 1)6h(id 6 over id 3 和 4)1h(id 7 over id 3 和 6)

我检查了这一点:使用 SQL 选择重叠的日期时间事件,但似乎没有做我需要的事情。

谢谢你。

4

2 回答 2

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;
于 2013-04-08T10:39:59.197 回答
0

我有一些“肮脏”的解决方案。希望这可以帮助 :)

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

于 2013-04-08T10:28:04.443 回答