2016-08-12 58 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