2016-08-12 60 views
0

表:计算天的差距

 
+-----------+--------------+------------+------------+ 
| RequestID | RequestStaus | StartDate | EndDate | 
+-----------+--------------+------------+------------+ 
|   1 | pending  | 9/1/2015 | 10/2/2015 | 
|   1 | in progress | 10/2/2015 | 10/20/2015 | 
|   1 | completed | 10/20/2015 | 11/3/2015 | 
|   1 | reopened  | 11/3/2015 | null  | 
|   2 | pending  | 9/5/2015 | 9/7/2015 | 
|   2 | in progress | 9/7/2015 | 9/25/2015 | 
|   2 | completed | 9/25/2015 | 10/7/2015 | 
|   2 | reopened  | 10/10/2015 | 10/16/2015 | 
|   2 | completed | 10/16/2015 | null  | 
+-----------+--------------+------------+------------+ 

我想计算打开了天,但排除天之间完成并重新开放。例如,RequestID 1的开放日期为(2015年3月11日 - 2015年9月9日)+(GetDate() - 11/3/2015),对于请求2,总天数为/ 2015 - 9/5/2015)+(2015/10/16 - 10/10/2015)。

我想会是这样的结果:

 
+-----------+-------------------------------+ 
| RequestID |   DaysOpened   | 
+-----------+-------------------------------+ 
|   1 | 63 + (getdate() - 11/3/2015) | 
|   2 | 38       | 
+-----------+-------------------------------+ 

我该如何解决这个问题?谢谢!

+0

你永远不会完成你的榜样 - 你所期望的结果为ID 1和2? – Hogan

+0

根据你的问题,对于RequestID 1,数据应该是(11/3/2015 - 9/1/2015)+(GetDate() - 11/3/2015)。不是吗? –

+0

@ Dance-Henry,是的,先生! – Meidi

回答

0

经过测试。效果很好。 :)

注: 1)我假设required result = (FirstCompleteEndDate - PendingStartDate)+(Sum of all the Reopen duration)

2)所以我使用的是自联接。表b提供了确切的completed记录,其紧接在每个RequestIDin process记录之后。表c提供了Sum of all the Reopen duration

--create tbl structure 
create table #test (RequestID int, RequestStatus varchar(20), StartDate date, EndDate date) 
go 


--insert sample data 
insert #test 
select 1,'pending','09/01/2015','10/2/2015' 
union all 
select 1,'in progress','10/2/2015','10/20/2015' 
union all 
select 1,'completed','10/20/2015','11/3/2015' 
union all 
select 1,'reopened','11/3/2015',null 
union all 
select 2,'pending','09/05/2015','9/7/2015' 
union all 
select 2,'in progress','09/07/2015','9/25/2015' 
union all 
select 2,'completed','9/25/2015','10/7/2015' 
union all 
select 2,'reopened','10/10/2015','10/16/2015' 
union all 
select 2, 'completed','10/16/2015','11/12/2015' 
union all 
select 2,'reopened','11/20/2015',null 



select * from #test 



--below is solution 
select a.RequestID, a.Startdate as [PendingStartDate], b.enddate as [FirstCompleteEndDate], c.startdate as [LatestReopenStartDate], 
     datediff(day,a.startdate,b.enddate)+c.ReopenDays as [days] from #test a 
join (
    select *, row_number()over(partition by RequestID,RequestStatus order by StartDate) as rid from #test 
) as b 
on a.RequestID = b.RequestID 
join (
    select distinct RequestID, RequestStatus, max(StartDate)over(partition by RequestID,RequestStatus) as StartDate, 
      Sum(Case when enddate is null then datediff(day,startdate,getdate()) 
        when enddate is not null then datediff(day,startdate,enddate) 
      end)over(partition by RequestID,RequestStatus) as [ReopenDays] 
    from #test 
    where RequestStatus = 'reopened' 

) as c 
on b.RequestID = c.RequestID 
where a.RequestStatus ='pending' and b.RequestStatus = 'completed' and b.rid = 1 

结果:

enter image description here

+0

有一个问题,如果我有多个完整的和重新开放的日期,我需要总结它们,我该怎么做? – Meidi

+0

例如: 2,'completed','9/25/2015','10/7/2015'; 2, '重新打开', '二零一五年十月十日', '2015年10月16日'; 2,'completed','10/16/2015','11/12/2015'; 2,'reopened','11/20/2015',null你想要什么结果 –

+0

是的,类似的,所以在你的例子中,我需要添加getdate() - '11/20/2015'。我不知道如何动态制作它,因为可能有多个重新打开和完成。谢谢你,先生! – Meidi