WorkAkkaunt

SIS PROP PRINYAt

Jul 25th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 4.08 KB | None | 0 0
  1. declare @dt1 datetime = '2019-07-18 00:00:00.000'
  2. declare @dt2 datetime = '2019-07-19 23:59:59.999'
  3.  
  4. declare @tm1 datetime = '19000101 01:00'
  5. declare @tm2 datetime = '19000101 23:59'
  6.  
  7.  
  8. declare @interval int = 12
  9.  
  10. declare @tasks table ([Id] uniqueidentifier)
  11.     insert into @tasks select  [Id]
  12.     from [oktell_cc_temp].[dbo].[A_Cube_CC_Cat_Task]
  13.     where isoutput = 0
  14.  
  15. declare @slist int = 2
  16. declare @xline int = 20
  17. declare @operators table (id uniqueidentifier)
  18. INSERT @OPERATORS VALUES('230FCDC7-5DCD-4126-9374-E1EEB4F1F165')
  19.  
  20. set datefirst 1
  21.  
  22. declare @mnog float
  23.  
  24. select @mnog = case @interval
  25.     when 60 then 23.999999999999
  26.     else 23.999999999999  -- по 60 мин
  27.     end
  28. if (@interval is null)
  29.     set @interval = 60
  30.  
  31. ---------------------------------------------------------------------------
  32. DECLARE @Filtered TABLE(idchain uniqueidentifier, callresult int, datetimestart datetime, lenqueue int)
  33. DECLARE @Temp0 TABLE(idchain uniqueidentifier,  suc int, unsuc int, [int] datetime)
  34. DECLARE @Temp TABLE([int] datetime, idchain uniqueidentifier, suc int, unsuc int)
  35.  
  36. INSERT INTO @Filtered
  37. select  idchain, callresult, datetimestart, lenqueue
  38.     from [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]
  39.     where isoutput = 0
  40.         and Datetimestart between @dt1 and @dt2
  41.         and TimeStart  between @tm1 and @tm2
  42.         and IDTask in (select * from @tasks)
  43.         and idchain is not null
  44.         and (IDOperator in (select * from @operators))
  45.  
  46. INSERT INTO @Temp0
  47. select  [Idchain],
  48.             [suc] = case when ([CallResult] in (5, 8)) then 1 else 0 end,
  49.             [unsuc] = case @slist
  50.                         when 1 then case when not([CallResult] = 5 or [CallResult] = 18) then 1 else 0 end
  51.                         when 2 then case when not ([CallResult] = 5 or [CallResult] = 18 or lenqueue <= @xline) then 1 else 0 end
  52.                         else 0
  53.                       end,                 
  54.             [int] = case                       
  55.                 when @interval = 1 then cast(floor(cast (datetimestart as float)) as datetime) 
  56.                 when @interval = 7 then cast(floor(cast (datetimestart as float) - datepart (dw, datetimestart) + 1) as datetime)      
  57.                 when @interval = 12 then cast (cast(datepart (yy, datetimestart) as nvarchar (4)) + '-' + cast(datepart (mm, datetimestart) as nvarchar (2)) + '-01' as datetime)
  58.                 else    
  59.                     cast(  
  60.                         ( floor( cast(datetimestart as float) * @mnog) / @mnog -
  61.                         floor(cast(datetimestart as float)) )
  62.                         as datetime)               
  63.                 end
  64.                                        
  65.     from
  66.     @Filtered t
  67.  
  68. INSERT INTO @Temp
  69. select min(int) [int], idchain, sum(suc) suc, sum(unsuc) unsuc
  70.     from
  71.     @Temp0 t
  72.     group by idchain
  73.  
  74. select  [int] = case @interval
  75.             when 60 then convert(nvarchar(5),[int],108) + '—' + convert(nvarchar(5),dateadd(hh,1,[int]),108)
  76.             when 1 then convert(nvarchar(5),[int],4) + ' ' + cast(datepart(yy,[int]) as nvarchar(4)) -- + ' г.'
  77.             when 7 then convert(nvarchar(5),[int] ,4) + '—' +
  78.                                 convert(nvarchar(5), dateadd(dd, 6, [int]) ,4)
  79.             when 12 then case datepart(mm, [int])
  80.                         when 1 then 'Январь '+ cast(datepart(yy, [int])as nvarchar(4))
  81.                         when 2 then 'Февраль '+ cast(datepart(yy, [int])as nvarchar(4))
  82.                         when 3 then 'Март '+ cast(datepart(yy, [int])as nvarchar(4))
  83.                         when 4 then 'Апрель '+ cast(datepart(yy, [int])as nvarchar(4))
  84.                         when 5 then 'Май '+ cast(datepart(yy, [int])as nvarchar(4))
  85.                         when 6 then 'Июнь '+ cast(datepart(yy, [int])as nvarchar(4))
  86.                         when 7 then 'Июль '+ cast(datepart(yy, [int])as nvarchar(4))
  87.                         when 8 then 'Август '+ cast(datepart(yy, [int])as nvarchar(4))
  88.                         when 9 then 'Сентябрь '+ cast(datepart(yy, [int])as nvarchar(4))
  89.                         when 10 then 'Октябрь '+ cast(datepart(yy, [int])as nvarchar(4))
  90.                         when 11 then 'Ноябрь '+ cast(datepart(yy, [int])as nvarchar(4))
  91.                         when 12 then 'Декабрь '+ cast(datepart(yy, [int])as nvarchar(4))
  92.                         end
  93.             else convert(nvarchar(5),[int],108) + '—' + convert(nvarchar(5),dateadd(mi,30,[int]),108)
  94.             end,
  95.         sum (suc+unsuc) [total],
  96.         sum (suc) [served],
  97.         sum (unsuc) [lost]
  98. from
  99. @Temp t
  100. group by t.[int]
  101. order by t.[int]
  102.  
  103. --DROP TABLE #temp
Advertisement
Add Comment
Please, Sign In to add comment