记录之间的总寿命

时间:2017-10-02 16:59:01

标签: sql sql-server sql-server-2005

我有一个存储项目状态时间的表,为了获得每个项目的生命周期,我只需要运行日期差异和我设置的SUM,当我从其他表中包含其他日期时问题就出现了称为“中断”,中断表如下:

|_ID_| |_PROJ_| |_COMMENTS_| |_START_| |_END_|
----
 5  --  1080  -- testing 1 --  9/12/2017 -- 9/20/2017
 6  --  1080  -- testing 2 --  9/12/2017 -- 9/20/2017
 7  --  1080  -- testing 3 --  9/20/2017 -- 9/20/2017

我需要从整个生命周期中获取中断时间,但在这种情况下,您会看到2个中断(5和6),从9月12日到2017年9月20日,总计(没有周末)6每个天,所以这些记录的正常总和是6天+ 6天+ 1天,总共13天,但实际上它应该是总共7天的中断,因为#5和6发生在同一时期,它应该即使例如#5是从9/12/17到9/19/17(5天)而#6是从9/11/17到9/20/17(7天),也应该是相同的情况,这应该是是7天(没有周末),如果我中断#7说从17/22/17到9/22/2017(1天)我应该总共7天+ 1天= 8天

希望我的措辞正确,

解决这个问题的最有效方法是什么?

提前致谢

EDITED

项目时间表(非常相似)

|_ID_| |_PROJ_| |_STATUS_| |_START_| |_END_|
----
 1  --  1080  -- 19 --  9/8/2017 -- 9/12/2017
 2  --  1080  -- 20 --  9/12/2017 -- 9/20/2017
 2  --  1080  -- 26 --  9/20/2017 -- 9/20/2017

注意:这些状态时间是连续的,不重叠,它是项目流程,只有在完成当前状态后才会跳转到新状态

1 个答案:

答案 0 :(得分:0)

我的问题并不完全清楚,特别是主要部分,即" to get a lifetime of each project"

如果输出错误,请重新解释您的问题。 我的脚本仍然可以帮助您找到解决方案。

我认为主要问题是找到" Total distinct interruption Time"每个项目

为此,您可以拥有一个永久日期日历表。在我的示例中,它是临时表。

  

示例数据

declare @Interruption table(_ID_ int,_PROJ_ int,_COMMENTS_ varchar(50),_START_ date,_END_ date)
insert into @Interruption VALUES
  (5  ,  1080  ,'testing 1','9/12/2017','9/20/2017')
 ,(6  ,  1080  ,'testing 2','9/12/2017','9/20/2017')
 ,(7  ,  1080  ,'testing 3','9/20/2017','9/20/2017')

 declare @project table(_ID_ int,_PROJ_ int,_STATUS_ int,_START_ date,_END_ date)
insert into @project values
 (1  ,1080 ,19,'9/8/2017',  '9/12/2017')
 ,(2  ,1080 ,20,'9/12/2017','9/20/2017')
 ,(2  ,1080 ,26,'9/20/2017','9/20/2017')
  

如何找到"完全不同的一天中断时间"不包括周末

主要技巧发生在这里。所以你必须只测试这个并让我知道。

create table #exmDate(datecol date primary key)
insert into #exmDate
select dateadd(day,c.rn,'01/01/2017')
from(
select row_number()over(order by number)rn from master..spt_values)c

select e.*,c._PROJ_ from #exmDate e
CROSS apply(select _PROJ_, min(_START_)_START_,max(_END_) _END_ from @Interruption group by _PROJ_)c
where datecol>=c._START_
and datecol<=c._END_
and  DATENAME(dw, datecol) not in('Friday','Saturday','Sunday')



drop table #exmDate

如果输出正确,那么您可以在上面评论&#34;选择&#34;按以下步骤进行,

create table #exmDate(datecol date)
insert into #exmDate
select dateadd(day,c.rn,'01/01/2017')
from(
select row_number()over(order by number)rn from master..spt_values)c

--select e.*,c._PROJ_ from #exmDate e
--CROSS apply(select _PROJ_, min(_START_)_START_,max(_END_) _END_ from @t group by _PROJ_)c
--where datecol>=c._START_
--and datecol<=c._END_
--and  DATENAME(dw, datecol) not in('Friday','Saturday','Sunday')

;with CTE as
(
select e.datecol,c._PROJ_ from #exmDate e
CROSS apply(select _PROJ_, min(_START_)_START_,max(_END_) _END_ from @Interruption group by _PROJ_)c
where datecol>=c._START_
and datecol<=c._END_
and  DATENAME(dw, datecol) not in('Friday','Saturday','Sunday')
)
,cte1 AS(
select _PROJ_ ,sum(DATEDIFF(day,_START_,_END_)) TotalProjectTime
from @project p
 group by p._PROJ_
)
select _PROJ_ ,TotalProjectTime -c.TotalInterruptiontime
from cte1 p
cross apply(select count(*)TotalInterruptiontime from CTE where _PROJ_=p._PROJ_ group by _proj_)c


drop table #exmDate