请帮助更改日期列中具有相同ID代码的每一行的工作时间,并减少空闲时间。所有输入都是动态的,可以根据用户键入的内容进行更改。
例如:
以此类推。
ID PROJ NAME STATUS DATE CHANGED
----------------------------------------------------
1800 abc COMPLETED 10/1/2017 14:48
1800 abc IN PROGRESS 10/1/2017 13:02
1800 abc ON-HOLD 10/1/2017 11:55
1800 abc IN PROGRESS 10/1/2017 8:07
1800 abc ON-HOLD 9/1/2017 18:29
1800 abc IN PROGRESS 9/1/2017 15:58
1800 abc PENDING DATA 9/1/2017 13:52
1800 abc IN PROGRESS 9/1/2017 13:04
1800 abc ON-HOLD 9/1/2017 11:53
1800 abc IN PROGRESS 9/1/2017 8:37
1800 abc ON-HOLD 9/1/2017 8:03
1800 abc START 9/1/2017 6:03
谢谢。
答案 0 :(得分:3)
检查此代码,我选择了每个正在进行的项目,然后找到了下一个日期时间,通过减去这两个日期时间,我找到了所有进度时间,并汇总了这些值,得出总的活动时间:
declare @s table (ID int, PROJ_NAME nvarchar(max), STATUS nvarchar(max), DATE_CHANGED datetime)
insert into @s (ID,PROJ_NAME,[status],DATE_CHANGED)
values
(1800 ,'abc', 'COMPLETED' ,'10/1/2017 14:48'),
(1800 ,'abc', 'IN PROGRESS' ,'10/1/2017 13:02'),
(1800 ,'abc', 'ON-HOLD' ,'10/1/2017 11:55'),
(1800 ,'abc', 'IN PROGRESS' ,'10/1/2017 8:07'),
(1800 ,'abc', 'ON-HOLD' ,'9/1/2017 18:29'),
(1800 ,'abc', 'IN PROGRESS' ,'9/1/2017 15:58'),
(1800 ,'abc', 'PENDING DATA' ,'9/1/2017 13:52'),
(1800 ,'abc', 'IN PROGRESS' ,'9/1/2017 13:04'),
(1800 ,'abc', 'ON-HOLD' ,'9/1/2017 11:53'),
(1800 ,'abc', 'IN PROGRESS' ,'9/1/2017 8:37'),
(1800 ,'abc', 'ON-HOLD' ,'9/1/2017 8:03'),
(1800 ,'abc', 'START' ,'9/1/2017 6:03')
select sum(datediff(second,date_changed,lead)) from (
select date_changed,LEAD(date_changed,1,0) over (order by date_changed
) as lead,[status] from @s
) D
where D.STATUS = 'IN PROGRESS'