2016-08-12 7 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年11月3日〜2015年9月1日)+(GetDate() - 11/3/2015)、リクエスト2の場合、合計日数は10/7/2015 - 9/5/2015)+(10/16/2015 - 10/10/2015)。私はこの問題にアプローチするにはどうすればよい

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

:私が欲しい

結果は次のようになりますか?ありがとうございました!

+0

あなたは決してあなたの例を完了 - あなたは、ID 1と2のためにどのような結果を期待していますか? – Hogan

+0

質問ごとに、RequestID 1のデータは(2015年11月3日〜2015年9月1日)+(GetDate() - 2015年11月3日)である必要があります。ではない? –

+0

@ Dance-Henry、はいサー! – Meidi

答えて

0

テスト済みです。うまくいく。 :)

注: 1)私は、だから私は自分が参加する使用)required result = (FirstCompleteEndDate - PendingStartDate)+(Sum of all the Reopen duration)

2を想定。表bは、それぞれのin processレコードの直後にある正確なcompletedレコードを提供します。表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、「再開」、「10/10/2015」、「10/16/2015」。 2、 'completed'、 '10/16/2015'、'11/12/2015 '; 2、 'reopened'、 '11/20/2015'、nullあなたが結果として欲しいものは –

+0

そうですね、あなたの例では、getdate() - '11/20/2015 'を追加する必要があります。私はそれを複数の再オープンと完了が可能なので動的にする方法を知らない。ありがとうございます! – Meidi