Advertisement
WorkAkkaunt

Код Виталия

Aug 29th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 15.97 KB | None | 0 0
  1. /*
  2. Доработал: Казанцев В.С.
  3. Дата: 17.08.2018   
  4. Изменения: Добавлены перезвоны по задачам: Транспортный привлечение=Перезвон Транспортный привлечение, B2D Premium==Перезвон b2d-premium
  5. B2D Night=Перезвон b2d-night, Driver-Class Courier=Перезвон Courier driver,
  6. User-class courier B2C\User-class courier B2B = Перезвон Courier (Общий на B2C и B2B)
  7. 24.09.2018 Казанцев В.С. - Для адекватного срабатывания условия ELSE добавлены промежуточные переменные datetime, заменены в параметрах имена.
  8. */
  9.  
  10. /*
  11. declare @D1 datetime;
  12. declare @D2  datetime;
  13. declare @T1 datetime;
  14. declare @T2  datetime;
  15.  
  16. set @D1 = '2018-12-11 00:00:00'
  17. set @D2  = '2018-12-11 23:59:00' -- не включительно.
  18. set @T1 = '1900-01-01 12:00:00'
  19. set @T2  = '1900-01-01 12:20:00'
  20. declare @TypeTime nvarchar(50)
  21. declare @TypeReport int=1
  22. */
  23.  
  24. declare @D1 datetime;
  25. declare @D2  datetime;
  26. declare @T1 datetime;
  27. declare @T2  datetime;
  28.  
  29. set @D1 = convert(datetime,@DT1,103)
  30. set @D2  = convert(datetime,@DT2,103)-- не включительно.
  31. set @T1 = convert(datetime,@TM1,103)
  32. set @T2 = convert(datetime,@TM2,103)
  33.  
  34. declare @TempTime datetime;
  35.  
  36. if (@TypeTime = 'True')
  37. begin
  38.     set @TempTime = @T1;
  39.     set @T1 = @T2;
  40.     set @T2 = @TempTime;
  41. end
  42.  
  43. if (@TypeReport is null)
  44.     set @TypeReport = 1;
  45.  
  46.  
  47. DECLARE @TABLE_ TABLE  ( CON_TYPE INT, USERID UNIQUEIDENTIFIER , [TASKID]  UNIQUEIDENTIFIER, [IN_TALK] INT,[IN_IVR] INT, [LENQUEUE] INT,waittime int)
  48.  
  49. IF (@TypeReport = 1)
  50. BEGIN
  51. --INSERT INTO @TABLE_ (CON_TYPE,USERID,[TASKID],[IN_TALK],[IN_IVR],[LENQUEUE],waittime)
  52. SELECT  [A_Stat_Connections_1x1].[ConnectionType] AS CON_TYPE
  53. ,[A_Stat_Connections_1x1_NEXT].[BUserId] AS USERID
  54. ,(SELECT  TOP(1) IdTask  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections] with(nolock)
  55. WHERE IdChain = [A_Stat_Connections_1x1].IdChain)/*[IdConn] = [A_Stat_Connections_1x1].Id)*/ AS [TaskId]
  56.  ,DATEDIFF(SS,[A_Stat_Connections_1x1_NEXT].[TimeAnswer],[A_Stat_Connections_1x1_NEXT].[TimeStop]) AS  [IN_TALK]
  57.  ,DATEDIFF(SS,[A_Stat_Connections_1x1].[TimeStart],[A_Stat_Connections_1x1].[TimeStOP]) AS [IN_IVR]
  58.  ,(SELECT TOP (1) LenQueue  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections] WHERE IdChain = [A_Stat_Connections_1x1].IdChain) AS LenQueue
  59. ,(select avg(datediff(ss,timestart,timestop)) from oktell.dbo.A_stat_Connections_1x1 where idchain =[A_Stat_Connections_1x1].idchain and bstr='ivr')
  60. +
  61. isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  62.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='319EC8C2-FE14-4C6C-AFA2-01FCBC718C14'),0)
  63.  +
  64.  isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  65.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='6830E035-0316-4799-9034-6CBBD6D89C4E'),0) qwe
  66.   into #TEMPGETTALL
  67.   FROM [oktell].[dbo].[A_Stat_Connections_1x1]  AS [A_Stat_Connections_1x1] with(nolock)
  68.   LEFT JOIN  [oktell].[dbo].[A_Stat_Connections_1x1] AS [A_Stat_Connections_1x1_NEXT]
  69.   ON [A_Stat_Connections_1x1_NEXT].[ID] = [A_Stat_Connections_1x1].IdNext
  70.   WHERE [A_Stat_Connections_1x1].[ConnectionType] in(4) and [A_Stat_Connections_1x1].IdPrev IS NULL
  71.   AND cast(A_Stat_Connections_1x1.TimeStart as date) between @D1 and @D2
  72.   and cast(cast(A_Stat_Connections_1x1.TimeStart as time)as datetime) between @T1 and @T2
  73. --Возможно зря убрал  
  74. --AND [A_Stat_Connections_1x1].[FromCC] = 1
  75.   UNION ALL
  76.   SELECT [A_Stat_Connections_1x1].[ConnectionType]
  77.   ,[A_Stat_Connections_1x1].[AUserId]
  78.   ,(SELECT  TOP(1) IdTask  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections] with(nolock)
  79.    WHERE IdChain = [A_Stat_Connections_1x1].IdChain)/*[IdConn] = [A_Stat_Connections_1x1].Id)*/ AS [TaskId]
  80.   ,DATEDIFF(SS,[TimeAnswer],[TimeStop])AS  [IN_TALK]
  81.   ,DATEDIFF(SS,[TimeStart],[TimeAnswer])AS  [IN_IVR]
  82.   ,(SELECT TOP (1) LenQueue  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections] with(nolock)
  83.   WHERE IdChain = [A_Stat_Connections_1x1].IdChain) AS LenQueue
  84. ,(select avg(datediff(ss,timestart,timestop)) from oktell.dbo.A_stat_Connections_1x1 where idchain =[A_Stat_Connections_1x1].idchain and bstr='ivr')
  85. +
  86. isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  87.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='319EC8C2-FE14-4C6C-AFA2-01FCBC718C14'),0)
  88.  +
  89.  isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  90.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='6830E035-0316-4799-9034-6CBBD6D89C4E'),0)
  91.    FROM [oktell].[dbo].[A_Stat_Connections_1x1] AS [A_Stat_Connections_1x1] with(nolock)
  92.   WHERE [A_Stat_Connections_1x1].[ConnectionType] IN(1) and [A_Stat_Connections_1x1].IdPrev IS NULL
  93.   AND cast(A_Stat_Connections_1x1.TimeStart as date) between @D1 and @D2
  94.   and cast(cast(A_Stat_Connections_1x1.TimeStart as time)as datetime) between @T1 and @T2
  95.   AND [A_Stat_Connections_1x1].[FromCC] = 1
  96. END
  97. ELSE
  98. BEGIN
  99. --INSERT INTO @TABLE_ (CON_TYPE,USERID,[TASKID],[IN_TALK],[IN_IVR],[LENQUEUE],waittime)
  100. SELECT  [A_Stat_Connections_1x1].[ConnectionType] AS [IVR]
  101. ,[A_Stat_Connections_1x1_NEXT].[BUserId] AS [BUserId]
  102. ,(SELECT  TOP(1) IdTask  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections] with(nolock)
  103. WHERE IdChain = [A_Stat_Connections_1x1].IdChain)/*[IdConn] = [A_Stat_Connections_1x1].Id)*/ AS [TaskId]
  104.  ,DATEDIFF(SS,[A_Stat_Connections_1x1_NEXT].[TimeAnswer],[A_Stat_Connections_1x1_NEXT].[TimeStop]) AS  [IN_TALK]
  105.  ,DATEDIFF(SS,[A_Stat_Connections_1x1].[TimeStart],[A_Stat_Connections_1x1].[TimeStOP]) AS [IN_IVR]
  106.  ,(SELECT TOP (1) LenQueue  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections] with(nolock) WHERE IdChain = [A_Stat_Connections_1x1].IdChain) AS LenQueue
  107. ,(select avg(datediff(ss,timestart,timestop)) from oktell.dbo.A_stat_Connections_1x1 where idchain =[A_Stat_Connections_1x1].idchain and bstr='ivr')
  108. +
  109. isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  110.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='319EC8C2-FE14-4C6C-AFA2-01FCBC718C14'),0)
  111.  +
  112.  isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  113.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='6830E035-0316-4799-9034-6CBBD6D89C4E'),0) qwe
  114.   into #TEMPGETTALL2
  115.   FROM [oktell].[dbo].[A_Stat_Connections_1x1] AS [A_Stat_Connections_1x1] with(nolock)
  116.   LEFT JOIN  [oktell].[dbo].[A_Stat_Connections_1x1] AS [A_Stat_Connections_1x1_NEXT] with(nolock)
  117.   ON [A_Stat_Connections_1x1_NEXT].[ID] = [A_Stat_Connections_1x1].IdNext
  118.   WHERE A_Stat_Connections_1x1.TimeStart between @D1 and @D2 and
  119.   [A_Stat_Connections_1x1].[ConnectionType] in(4) and [A_Stat_Connections_1x1].IdPrev IS NULL
  120. --Возможно зря убрал  
  121. --AND [A_Stat_Connections_1x1].[FromCC] = 1
  122.   UNION ALL
  123.   SELECT [A_Stat_Connections_1x1].[ConnectionType]
  124.  
  125.   ,[A_Stat_Connections_1x1].[AUserId]
  126.   ,(SELECT  TOP(1) IdTask  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections]with(nolock)
  127.   WHERE IdChain = [A_Stat_Connections_1x1].IdChain)/*[IdConn] = [A_Stat_Connections_1x1].Id)*/ AS [TaskId]
  128.  
  129.   ,DATEDIFF(SS,[TimeAnswer],[TimeStop])AS  [IN_TALK]
  130.   ,DATEDIFF(SS,[TimeStart],[TimeAnswer])AS  [IN_IVR]
  131.   ,(SELECT TOP (1) LenQueue  FROM [oktell_cc_temp].[dbo].[A_Cube_CC_EffortConnections]AS [A_Cube_CC_EffortConnections]  with(nolock)
  132.   WHERE IdChain = [A_Stat_Connections_1x1].IdChain) AS LenQueue
  133. ,(select avg(datediff(ss,timestart,timestop)) from oktell.dbo.A_stat_Connections_1x1 where idchain =[A_Stat_Connections_1x1].idchain and bstr='ivr')
  134. +
  135. isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  136.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='319EC8C2-FE14-4C6C-AFA2-01FCBC718C14'),0)
  137.  +
  138.  isnull((select avg(datediff(ss,datetimestart,datetimestop)) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where idchain in(select distinct idchain from oktell.dbo.tbl_log_twotasks where dt between @D1 and @D2
  139.  and cast(cast(dt as time(0)) as datetime) between @T1 and @T2) and IdOperator='ab000000-0000-0000-0000-000000000000 ' and IdTask='6830E035-0316-4799-9034-6CBBD6D89C4E'),0)
  140.    FROM [oktell].[dbo].[A_Stat_Connections_1x1] AS [A_Stat_Connections_1x1] with(nolock)
  141.   WHERE  [A_Stat_Connections_1x1].[ConnectionType] IN(1) and [A_Stat_Connections_1x1].IdPrev IS NULL
  142.   AND A_Stat_Connections_1x1.TimeStart between @D1 and @D2
  143.   AND [A_Stat_Connections_1x1].[FromCC] = 1
  144. END
  145.  
  146.  
  147. IF OBJECT_ID('tempdb..#TEMPGETTALL', 'U') IS NOT NULL
  148. begin
  149. DELETE #TEMPGETTALL WHERE [TASKID] IS NULL
  150. truncate table oktell.[dbo].[olp_report_stat_all]
  151. insert into oktell.[dbo].[olp_report_stat_all] select * from #TEMPGETTALL
  152. DROP TABLE #TEMPGETTALL
  153. end
  154. else
  155. --IF OBJECT_ID('#TEMPGETTALL2', 'U') IS NOT NULL
  156. begin
  157. DELETE #TEMPGETTALL2 WHERE [TASKID] IS NULL
  158. truncate table oktell.[dbo].[olp_report_stat_all]
  159. insert into oktell.[dbo].[olp_report_stat_all] select * from #TEMPGETTALL2
  160. DROP TABLE #TEMPGETTALL2
  161. end
  162.  
  163.  
  164. declare @max_res int
  165. set @max_res =  (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE
  166. [TaskId] IN ('D2A5DC6B-1522-415F-8892-9FF8D362E011','1A930507-4D94-4BE2-8B43-1BE99846C76B','27D3E314-A989-41D7-9B6D-D9E8FA347CC1','5C23239B-D4B2-4E8F-9FAF-CA61AA34792D'
  167. ,'48657090-561F-4C9C-8F67-64FB19C9CD08','651BC4E1-F838-442A-B2C9-BD89D350537E','467388EF-77CD-4928-8568-5CEEF9CCDCBC',
  168. '3A8C638D-1583-4951-8050-E272275DF1AD','951DBBBB-7383-487A-A3E7-B745D1F15BD0','C3351AFC-B114-4CF8-807D-F3561B261F90','21F63D17-0345-4CCE-B9E1-5027053104F2','8FD43D4D-D38E-41BA-A37E-0F531AE6D265'
  169. ,'B1014253-FEC2-497D-AF6E-9F11B240384B'
  170. ) )
  171.  
  172. declare @temp25 table (idtask uniqueidentifier, cnt int)
  173.  
  174. if (@TypeReport = 1)
  175. begin
  176. insert into @temp25
  177. select distinct TASKID,(select count(distinct idchain) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where DateTimeStart between @D1 and @D2
  178. and cast(cast(DateTimeStart as time(0)) as datetime) between @T1 and @T2 and CallResult = 25 and IdTask = TASKID) from oktell.[dbo].[olp_report_stat_all]
  179. end
  180. else
  181. begin
  182. insert into @temp25
  183. select distinct TASKID,(select count(distinct idchain) from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with(nolock) where DateTimeStart between @D1 and @D2
  184. and CallResult = 25 and IdTask = TASKID) from oktell.[dbo].[olp_report_stat_all]
  185. end
  186.  
  187. SELECT
  188. [A_TaskManager_Tasks].[Name] AS [Задача]
  189. ,COUNT(CON_TYPE) AS [Поступило]
  190. ,COUNT(USERID) AS [Принято]---(select cnt from @temp25 where idtask = RESULT_IN.TASKID)  AS [Принято]
  191. ,COUNT(CON_TYPE)-COUNT(USERID)  AS [Пропущено]
  192. ,(select cnt from @temp25 where idtask = RESULT_IN.TASKID) [Пропущено по таймауту]
  193. ,CAST(DATEADD(SS,CASE WHEN COUNT(USERID)>0 THEN SUM([IN_TALK])/COUNT(USERID)  ELSE 0 END,'1900-01-01 00:00:00') AS TIME(0)) AS [Среднее время в разговоре]
  194. ,CAST(DATEADD(SS,CASE WHEN COUNT(CON_TYPE)  >0 THEN SUM([IN_IVR])/COUNT(CON_TYPE)ELSE 0 END,'1900-01-01 00:00:00') AS TIME(0))  AS [Среднее время в IVR]
  195. ,CASE  [A_TaskManager_Tasks].[Name]
  196. WHEN '1 Клиентская линия (English)' THEN
  197. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = 'D2A5DC6B-1522-415F-8892-9FF8D362E011')
  198. WHEN '3 Корпоративные клиенты' THEN
  199. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '1A930507-4D94-4BE2-8B43-1BE99846C76B')
  200. WHEN '5 Vip-премиум' THEN
  201. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '27D3E314-A989-41D7-9B6D-D9E8FA347CC1')
  202. WHEN 'Транспортный привлечение' THEN
  203. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '651BC4E1-F838-442A-B2C9-BD89D350537E')
  204. WHEN 'B2C In ride' THEN
  205. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '48657090-561F-4C9C-8F67-64FB19C9CD08')
  206. WHEN 'B2C Not in ride' THEN
  207. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '467388EF-77CD-4928-8568-5CEEF9CCDCBC')
  208. WHEN 'B2B in ride' THEN
  209. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '3A8C638D-1583-4951-8050-E272275DF1AD')
  210. WHEN 'B2D in corp ride' THEN
  211. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '951DBBBB-7383-487A-A3E7-B745D1F15BD0')
  212. WHEN '6 Tap Taxi' THEN
  213. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '5C23239B-D4B2-4E8F-9FAF-CA61AA34792D')
  214. WHEN 'B2D Premium' THEN
  215. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = 'C3351AFC-B114-4CF8-807D-F3561B261F90')
  216. WHEN 'B2D Night' THEN
  217. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '21F63D17-0345-4CCE-B9E1-5027053104F2')
  218. WHEN 'User-class courier B2C' THEN
  219. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '8FD43D4D-D38E-41BA-A37E-0F531AE6D265')
  220. WHEN 'Driver-Class Courier' THEN
  221. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = 'B1014253-FEC2-497D-AF6E-9F11B240384B')
  222. WHEN 'User-Class Courier B2B' THEN
  223. (SELECT COUNT(*) FROM oktell.[dbo].[olp_report_stat_all] WHERE [TaskId] = '8FD43D4D-D38E-41BA-A37E-0F531AE6D265')
  224. END  AS [Перезвоны]
  225. ,cast(cast(DATEADD(SS,cast(avg(lenqueue)as int),'00:00:00')as datetime) as time(0)) [Время ожидания]
  226. FROM oktell.[dbo].[olp_report_stat_all] AS [RESULT_IN]
  227. LEFT JOIN [oktell_settings].[dbo].[A_TaskManager_Tasks] AS [A_TaskManager_Tasks] with(nolock)
  228. ON [RESULT_IN].[TaskId] = [A_TaskManager_Tasks].[Id]
  229. WHERE [RESULT_IN].CON_TYPE = 4
  230. GROUP BY [A_TaskManager_Tasks].[Name],result_in.taskid
  231. UNION ALL
  232. SELECT
  233. 'Итого'
  234. ,COUNT(CON_TYPE)
  235. ,COUNT(USERID)  - (select sum(cnt) from @temp25)
  236. ,COUNT(CON_TYPE)-COUNT(USERID)  
  237. ,(select sum(cnt) from @temp25)
  238. ,CAST(DATEADD(SS,CASE WHEN COUNT(USERID)>0 THEN SUM([IN_TALK])/COUNT(USERID)  ELSE 0 END,'1900-01-01 00:00:00') AS TIME(0)) AS [Среднее время в разговоре]
  239. ,CAST(DATEADD(SS,CASE WHEN COUNT(CON_TYPE)  >0 THEN SUM([IN_IVR])/COUNT(CON_TYPE)ELSE 0 END,'1900-01-01 00:00:00') AS TIME(0))  AS [Среднее время в IVR]
  240. ,@max_res
  241. ,cast(cast(DATEADD(SS,cast(avg(lenqueue)as int),'00:00:00')as datetime) as time(0))
  242. FROM oktell.[dbo].[olp_report_stat_all] AS [RESULT_IN]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement