WorkAkkaunt

temp

Jul 31st, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 KB | None | 0 0
  1. declare @dt1 datetime = '20190729 00:00'
  2. declare @dt2 datetime = '20190730 23:59'
  3.  
  4. declare @tm1 datetime = '19000101 00:00'
  5. declare @tm2 datetime = '19000101 23:00'
  6.  
  7. declare @task TABLE (Id uniqueidentifier)
  8. INSERT INTO @task
  9. SELECT id
  10. FROM [oktell_settings].[dbo].[A_TaskManager_Tasks]
  11.  
  12. declare @operators TABLE (Id uniqueidentifier)
  13. INSERT INTO @operators
  14. SELECT id
  15. FROM [oktell_settings].[dbo].[A_Users]
  16.  
  17. declare @tmp table(idoperator uniqueidentifier, summary int, success int, failed int, lentime float,
  18. v1 int, v2 int, v3 int, v4 int, v5 int, v6 int, v7 int, v8 int, v9 int, v10 int, v11 int, wd int)
  19.  
  20. declare @temp table (ideffort uniqueidentifier,idchain uniqueidentifier,idoperator uniqueidentifier, [CallResult] int, UserResult int, datetimestart datetime, lenTime float)
  21.  
  22. if ((select top 1 [type] from [oktell_cc_temp].[dbo].[A_Cube_CC_Cat_Task]
  23. where id in (select * from @task)) = 10)
  24. begin
  25. /*--------------------------------------------------*/
  26. /*--------------------- IVR ------------------------*/
  27. /*--------------------------------------------------*/
  28.  
  29. if (not exists (select * from [oktell_cc_temp].[dbo].[A_Cube_CC_Cat_OperatorInfo]
  30. where id = 'AB000000-0000-0000-0000-000000000000'))
  31. insert into [oktell_cc_temp].[dbo].[A_Cube_CC_Cat_OperatorInfo]
  32. values ('AB000000-0000-0000-0000-000000000000','IVR',0,0,0,0,0,0,null,null,null,null,null,null,getdate())
  33.  
  34. insert into @temp
  35. select Ideffort, idchain, idoperator, [CallResult], UserResult, Datetimestart, lenTime
  36. from [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections] ef1
  37. where ef1.Datetimestart between @dt1 and @dt2
  38. and ef1.TimeStart between @tm1 and @tm2
  39. and IDTask in (select * from @task)
  40. and ((IdOperator is null and (callresult in (13,6,7, 19, 23,24,26,27,30))) or IdOperator in (select id from @operators))
  41.  
  42. end
  43. else
  44. begin
  45.  
  46. if ((select top 1 isoutput from [oktell_cc_temp].[dbo].[A_Cube_CC_Cat_Task]
  47. where id in (select * from @task)) = 1)
  48. begin
  49. /*--------------------------------------------------*/
  50. /*------------------- ИСХОДЯЩИЕ --------------------*/
  51. /*--------------------------------------------------*/
  52. insert into @temp
  53. select tb1.[Ideffort], idchain, tb1.idoperator, [CallResult], UserResult, Datetimestart, lenTime
  54. from (
  55. select ef1.[Ideffort], min(datetimestart) dt, ef1.IdOperator
  56. from [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections] ef1
  57. where isoutput = 0
  58. and ef1.Datetimestart between @dt1 and @dt2
  59. and ef1.TimeStart between @tm1 and @tm2
  60. and IDTask in (select * from @task)
  61. and ((IdOperator is null and (callresult in (13,7)))
  62. or IdOperator in (select id from @operators))
  63. Group By ef1.IdEffort, IdOperator
  64.  
  65. ) tb1
  66. inner join
  67. (select ideffort, idchain, idoperator, [CallResult], UserResult, datetimestart,lenTime
  68. from [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]
  69. where idchain is not null or callresult in (2,3,7,13,6)
  70. ) ef3
  71. on (tb1.ideffort = ef3.ideffort) and (tb1.dt = ef3.Datetimestart) and ((tb1.idoperator = ef3.idoperator) or (( tb1.idoperator is NULL ) and ( ef3.idoperator is NULL )))
  72.  
  73. end
  74. else
  75. begin
  76. /*--------------------------------------------------*/
  77. /*------------------- ВХОДЯЩИЕ ---------------------*/
  78. /*--------------------------------------------------*/
  79. insert into @temp
  80. select tb1.[Ideffort], idchain, tb1.idoperator, [CallResult], UserResult, Datetimestart, lenTime
  81. from (
  82. select ef1.[Ideffort], min(datetimestart) dt, ef1.IdOperator
  83. from [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections] ef1
  84. where isoutput = 0
  85. and ef1.Datetimestart between @dt1 and @dt2
  86. and ef1.TimeStart between @tm1 and @tm2
  87. and IDTask in (select * from @task)
  88. and ((IdOperator is null and (callresult in (13,6, 19, 23,24,26,27,30)))
  89. or IdOperator in (select id from @operators))
  90. Group By ef1.IdEffort, IdOperator
  91. ) tb1
  92. inner join
  93. ( select ideffort, idchain, idoperator, [CallResult], UserResult, datetimestart, lentime
  94. from [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]
  95. where idchain is not null or callresult in (19,13,6)
  96. ) ef3
  97. on (tb1.ideffort = ef3.ideffort) and (tb1.dt = ef3.Datetimestart) and ((tb1.idoperator = ef3.idoperator) or (( tb1.idoperator is NULL ) and ( ef3.idoperator is NULL )))
  98.  
  99.  
  100. end
  101. end
  102.  
  103. --select * from @temp
  104. --order by datetimestart
  105.  
  106. insert into @tmp
  107. select IdOperator,
  108. count (cast (ideffort as nvarchar(50))),
  109. sum(p0) v0,
  110. sum(p00) v00,
  111. avg(lenTime),
  112. sum(p1) v1,
  113. sum(p2) v2,
  114. sum(p3) v3,
  115. sum(p4) v4,
  116. sum(p5) v5,
  117. sum(p6) v6,
  118. sum(p7) v7,
  119. sum(p8) v8,
  120. sum(p9) v9,
  121. sum(p10) v10,
  122. sum(p11) v11,
  123. 0
  124. from
  125. (
  126. select ideffort , IdOperator, DateTimeStart, lenTime,
  127. case when CallResult=5 or CallResult=18 then 1 else 0 end as p0,
  128. case when CallResult != 5 and CallResult != 18 then 1 else 0 end as p00,
  129. case when UserResult=1 then 1 else 0 end as p1,
  130. case when UserResult=2 then 1 else 0 end as p2,
  131. case when UserResult=3 then 1 else 0 end as p3,
  132. case when UserResult=4 then 1 else 0 end as p4,
  133. case when UserResult=5 then 1 else 0 end as p5,
  134. case when UserResult=6 then 1 else 0 end as p6,
  135. case when UserResult=7 then 1 else 0 end as p7,
  136. case when UserResult=8 then 1 else 0 end as p8,
  137. case when UserResult=9 then 1 else 0 end as p9,
  138. case when UserResult=10 then 1 else 0 end as p10,
  139. case when UserResult=11 then 1 else 0 end as p11
  140. from @temp
  141. ) t
  142. group by IdOperator
  143.  
  144. update @tmp
  145. set wd = daycnt
  146. from
  147. (
  148. select IdOperator id, count(distinct DateStart) daycnt
  149. from oktell_cc_temp..A_Cube_CC_OperatorStates os
  150. where DateStart between @dt1 and @dt2
  151. and cast(
  152. cast ( cast('1900-01-01' as datetime) as float) +
  153. cast(TimeStart as float) - floor(cast(TimeStart as float))
  154. as datetime) between @tm1 and @tm2
  155. group by idoperator) t
  156. where idoperator=t.Id
  157.  
  158. --select * from @tmp
  159.  
  160. declare @sc int
  161. declare @fl int
  162. declare @sm int
  163.  
  164. select @sc = count (distinct (cast(idchain as nvarchar(40))) )
  165. from @temp
  166. where CallResult = 5 or CallResult = 18
  167.  
  168. select @fl = count (distinct (cast(idchain as nvarchar(40))) )
  169. from @temp
  170. where CallResult != 5 and CallResult != 18
  171. and idchain not in (select idchain
  172. from @temp
  173. where CallResult = 5 or CallResult = 18 )
  174.  
  175. select @sm = @fl + @sc
  176.  
  177.  
  178. select case when ( t.Idoperator = 'AB000000-0000-0000-0000-000000000000' ) then 'IVR' else isnull(oi.Name,'Потеряно по задаче') end Name,
  179. t.summary,
  180. t.success,
  181. t.failed,
  182. case when t.failed + t.success = 0 then 100 else round(t.success*100/(t.success+t.failed), 0) end prc,
  183. case when t.failed + t.success = 0 then 0 else round(t.failed*100/(t.success+t.failed), 0) end prcfailed,
  184. case
  185. when oi.Name is null then null
  186. else (cast(floor(t.lentime/3600) as nvarchar (10)))
  187. + ':' +
  188. case
  189. when floor(t.lentime/60) < 10 then ('0'+ cast(floor(t.lentime/60) as nvarchar (10)))
  190. when floor(t.lentime/60) >= 10 then (cast(floor(t.lentime/60) as nvarchar (10)))
  191. end + ':' +
  192. case
  193. when floor(t.lentime) - floor(t.lentime/60)*60 < 10 then ('0'+cast(floor(t.lentime) - floor(t.lentime/60)*60 as nvarchar (10)))
  194. when floor(t.lentime) - floor(t.lentime/60)*60 >= 10 then (cast(floor(t.lentime) - floor(t.lentime/60)*60 as nvarchar (10)))
  195. end
  196. end lentime,
  197. case when oi.Name is null then null
  198. else t.v1 end as [v1],
  199. case when oi.Name is null then null
  200. else t.v2 end as [v2],
  201. case when oi.Name is null then null
  202. else t.v3 end as [v3],
  203. case when oi.Name is null then null
  204. else t.v4 end as [v4],
  205. case when oi.Name is null then null
  206. else t.v5 end as [v5],
  207. case when oi.Name is null then null
  208. else t.v6 end as [v6],
  209. case when oi.Name is null then null
  210. else t.v7 end as [v7],
  211. case when oi.Name is null then null
  212. else t.v8 end as [v8],
  213. case when oi.Name is null then null
  214. else t.v9 end as [v9],
  215. case when oi.Name is null then null
  216. else t.v10 end as [v10],
  217. case when oi.Name is null then null
  218. else t.v11 end as [v11],
  219. case when oi.Name is null then null
  220. else t.wd end as [wd]
  221. from @tmp t
  222. left join oktell_cc_temp..A_Cube_CC_Cat_OperatorInfo oi
  223. on oi.Id=t.idoperator
  224. union all
  225. Select 'Всего', @sm, @sc, @fl,
  226. case when @sm = 0 then 100 else round(@sc*100/@sm,0) end,
  227. case when @sm = 0 then 0 else 100-round(@sc*100/@sm,0) end,
  228. null,
  229. sum(v1), sum(v2), sum(v3), sum(v4), sum(v5), sum(v6), sum(v7), sum(v8), sum(v9), sum(v10), sum(v11),null
  230. from @tmp
Add Comment
Please, Sign In to add comment