从一系列重叠的开始/结束时间戳计算每天花费的小时数

时间:2015-08-18 19:52:39

标签: datetime plsql timestamp

我目前有一些产生的代码:

1 Divert ----- 20-JUL-2015 10:25:15 ------ 21-JUL-2015 11:46:01
1 Divert ----- 27-JUL-2015 08:42:24 ------ 27-JUL-2015 03:13:23
1 Divert ----- 2015年8月15日08:41:19 ----- 2015年8月3日03:21:48

我希望它能够生产 标题-----日期-----转移时间小时
1 Divert-----20-JUL-2015------14.XXX
1 Divert-----21-JUL-2015------11.XXX
1 Divert-----27-JUL-2015------7.XXX
1 Divert-----15-AUG-2015-----14.XXX
1 Divert-----16-AUG-2015-----24.XXX
1 Divert-----17-AUG-2015-----15.XXX

这是我目前的代码:

    with starts as
    (
      select 
        dep.DEPARTMENT_ID,
        dsts.DEP_STS_NAME_C,
        EVENT_SOURCE_CSN_ID,
        zc.NAME as STATUS,
        sts.EVENT_TIME as START_TIME,
        (select min(EVENT_TIME) 
        from ED_IEV_EVENT_INFO 
        where EVENT_TYPE=dsts.DEP_STS_ACT_EV_ID 
        and EVENT_TIME>sts.EVENT_TIME) as NEXT_START_TIME
      from 
        CLARITY_DEP dep
        inner join DEP_STATUS dsts on dep.DEPARTMENT_ID=dsts.DEPARTMENT_ID
        inner join ZC_DEP_STS_NAME zc on dsts.DEP_STS_NAME_C=zc.DEP_STS_NAME_C
        inner join ED_IEV_EVENT_INFO sts on sts.EVENT_TYPE=dsts.DEP_STS_ACT_EV_ID
      where
        sts.EVENT_TIME>EPIC_UTIL.EFN_DIN('t-30')
    ),
    end_times as
    (
      select 
        starts.DEPARTMENT_ID,
        starts.STATUS,
        starts.START_TIME,
        starts.NEXT_START_TIME,
        (select min(sts.EVENT_TIME) 
        from DEP_STATUS dsts 
        inner join ED_IEV_EVENT_INFO sts on dsts.DEP_STS_DEACT_EV_ID=sts.EVENT_TYPE 
        where sts.EVENT_TIME>starts.START_TIME 
        and (starts.NEXT_START_TIME is null 
        or (starts.NEXT_START_TIME>sts.EVENT_TIME)) ) as END_TIME
      from
        starts
    )

    select 
      dep.DEPARTMENT_ID,
      dep.DEPARTMENT_NAME,
      end_times.STATUS,
      TO_CHAR(end_times.START_TIME, 'DD-MON-YYYY HH:MI:SS') as ACTIVATION,
      TO_CHAR(coalesce(end_times.END_TIME,(end_times.NEXT_START_TIME - (INTERVAL '1' SECOND))) , 'DD-MON-YYYY HH:MI:SS') as DEACTIVATION
    from end_times
      left join clarity_dep dep on end_times.DEPARTMENT_ID=dep.DEPARTMENT_ID
      ;

1 个答案:

答案 0 :(得分:0)

试试这个:

样本数据

Drop table dbo.timeTable

Create table dbo.timeTable (type nvarchar(100), startdate datetime, EndDate datetime)

Insert into dbo.timeTable values ('type1','20-JUL-2015 10:25:15 AM','21-JUL-2015 11:46:01 PM');
Insert into dbo.timeTable values ('type1','27-JUL-2015 08:42:24 AM','27-JUL-2015 03:13:23 PM');
Insert into dbo.timeTable values ('type1','15-AUG-2015 08:41:19 AM','17-AUG-2015 03:21:48 PM');

数据之前:

Select * from dbo.timeTable

enter image description here

数据后:

    declare @mindate date
Select @mindate = Min(startDate) from dbo.timeTable

; with CTE_splt
as (
Select number, dateadd(dd, number, @mindate) splitdate from master..spt_values Where type = 'P'
) 

Select Type, splitdate, 
Cast( (Case When Cast(a.StartDate as Date) = Cast(a.EndDate as Date) Then Datediff(MI,a.StartDate,a.EndDate) 
When splitdate = Cast(a.StartDate as Date) Then Datediff(MI,a.StartDate,Dateadd(dd,1,splitdate)) 
When splitdate = Cast(a.EndDate as Date) Then Datediff(MI,splitdate,a.EndDate)
Else 24*60
End) as Float) / 60 
from dbo.timeTable a inner join CTE_splt b 
ON b.Splitdate between Cast(a.StartDate as Date) and Cast(a.EndDate as Date)
Order by Splitdate

enter image description here