WorkAkkaunt

Северсталь Рабочее время оперов В1

Jul 30th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.95 KB | None | 0 0
  1. select * into #operators
  2.       from @operators
  3. if ( @@rowcount = 0 )
  4.     insert into #operators
  5.     SELECT id
  6.     FROM [oktell_cc_temp].[dbo].[A_Cube_CC_Cat_OperatorInfo]
  7.  
  8. select * into #tasks
  9.       from @tasks
  10. if ( @@rowcount = 0 )
  11.     insert into #tasks
  12.         Select id
  13.   FROM [oktell_cc_temp].[dbo].[A_Cube_CC_Cat_Task]
  14.  
  15.  
  16. create table #temp(IdOperator uniqueidentifier, IdTask uniqueidentifier, State int, LenTime float)
  17.  
  18. insert into #temp
  19. select IdOperator, IdTask, State, LenTime
  20. from(
  21.     select IdOperator, IdTask, State,
  22.         case when DateStart = DateStop then cast(TimeStop-TimeStart as float)
  23.         else (DateDiff(day, DateStart, DateStop)-1)*cast((@tm2-@tm1) as float)+cast((@tm2-TimeStart)+(TimeStop-@tm1) as float) end LenTime
  24.     from(
  25.         select IdOperator, IdTask, State, DateStart,
  26.             case when DateTimeStart-DateStart<@tm1 then @tm1 else DateTimeStart-DateStart end TimeStart,
  27.             DateStop,
  28.             case when DateTimeStop-DateStop>@tm2 then @tm2 else DateTimeStop-DateStop end TimeStop, DateTimeStart d1, datetimestop d2
  29.         from(
  30.             select IdOperator, IdTask, State, DateTimeStart,
  31.                 case when DateStart<@dt1 then @dt1 else DateStart end DateStart,
  32.                 DateTimeStop,
  33.                 case when dateadd(day, datediff(day, 0, DateTimeStop),0)>@dt2 then @dt2 else dateadd(day, datediff(day, 0, DateTimeStop),0) end DateStop
  34.             from
  35.             [oktell_cc_temp].[dbo].[A_Cube_CC_OperatorStates] os
  36.             where DateTimeStart<=dateadd(day, datediff(day, 0, @dt2),0)+@tm2 and DateTimeStop>=@dt1+@tm1
  37.             and IdOperator in (select id from #operators)
  38.         )t)t)t
  39. where LenTime>0
  40.  
  41.  
  42. select oi.Name,
  43.     round(task*86400, 1) task,
  44.     cast(floor(task*24) as nvarchar(10)) + stuff(convert(nvarchar(10), cast(task as datetime), 108), 1, 2, '') taskLabel,
  45.     round(othertask*86400, 1) othertask,
  46.     cast(floor(othertask*24) as nvarchar(10)) + stuff(convert(nvarchar(10), cast(othertask as datetime), 108), 1, 2, '') othertaskLabel,
  47.     round(talk*86400, 1) talk,
  48.     cast(floor(talk*24) as nvarchar(10)) + stuff(convert(nvarchar(10), cast(talk as datetime), 108), 1, 2, '') talkLabel,
  49.     round(rest*86400, 1) rest,
  50.     cast(floor(rest*24) as nvarchar(10)) + stuff(convert(nvarchar(10), cast(rest as datetime), 108), 1, 2, '') restLabel,
  51.     round(ready*86400, 1) ready,
  52.     cast(floor(ready*24) as nvarchar(10)) + stuff(convert(nvarchar(10), cast(ready as datetime), 108), 1, 2, '') readyLabel
  53. from(  
  54.     select a.IdOperator,
  55.         sum(case when State in (1,2,3,4,6,7,13,14) and t.id is not null then LenTime else 0 end) task,
  56.         sum(case when State in (1,2,3,4,6,7,13,14) and t.id is null then LenTime else 0 end) othertask,
  57.         sum(case when State in (9) then LenTime else 0 end) rest,
  58.         sum(case when State in (10,12) then LenTime else 0 end) ready,
  59.         sum(case when State in (5,8) then LenTime else 0 end) talk
  60.     from #temp a
  61.     left join #tasks t
  62.     on a.IdTask=t.id
  63.     group by a.IdOperator)t
  64. inner join
  65.     oktell_cc_temp..A_Cube_CC_Cat_OperatorInfo oi
  66. on oi.Id=t.IdOperator
  67. order by oi.Name
  68.  
  69. drop table #operators
  70. drop table #temp
  71. drop table #tasks
Advertisement
Add Comment
Please, Sign In to add comment