Advertisement
WorkAkkaunt

ОДФ Исход

Jul 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 5.90 KB | None | 0 0
  1. DECLARE @DateStart datetime;
  2. DECLARE @DateEnd   datetime;
  3. DECLARE @TimeStart datetime;
  4. DECLARE @TimeEnd   datetime;
  5.  
  6. SET @DateStart = '20190718 00:00:00.000'
  7. SET @DateEnd = '20190718 23:59:00.000'
  8. SET @TimeStart = '19000101 00:00:00.000'
  9. SET @TimeEnd = '19000101 23:59:00.000'
  10.  
  11. DECLARE @tasks TABLE (id uniqueidentifier)
  12. DECLARE @opers TABLE ( id uniqueidentifier)
  13.  
  14. INSERT INTO @opers
  15. SELECT DISTINCT id FROM oktell.dbo.A_Users
  16.  
  17. INSERT INTO @tasks
  18. SELECT id FROM oktell.dbo.A_TaskManager_Tasks WHERE name ='B2B in ride'
  19.  
  20. DECLARE @AbonentNumber nvarchar(250);
  21. SET @AbonentNumber = ''
  22. DECLARE @type int = 0
  23.  
  24. --------------------------------------------------------------------------------------------------------------------------
  25.    
  26. SELECT od.IdChain, datewrite, callerID, od.idtask, Operator, Casy_type, Sub_type,Sub_3, [result], num_drive
  27. INTO #dialogtabletemp
  28. FROM oktell..Olp_dialog od
  29. LEFT JOIN oktell.dbo.A_Users au
  30.   ON au.name=od.Operator --Старый джойн, возможно пригодится, заменен на eff
  31. LEFT JOIN oktell_cc_temp.dbo.A_Cube_CC_EffortConnections eff with (nolock)
  32.     ON eff.IdChain=od.IdChain
  33. WHERE od.DateWrite BETWEEN @DateStart AND @DateEnd
  34.     AND CAST(cast(od.DateWrite as time)AS datetime) BETWEEN @TimeStart AND @TimeEnd
  35.     AND od.idtask IN (SELECT id FROM @tasks)
  36.     AND au.ID IN (select id from @opers)
  37.     AND eff.IdOperator IN (SELECT id FROM @opers)
  38.     AND CallResult NOT IN (6, 7, 13) -- строка добавлена по AI-11600 14.11.2018, раньше было без нее
  39.  
  40. SELECT DISTINCT D1.IdChain
  41.     , D2.DateWrite
  42.     , D1.CallerID
  43.     , D2.idtask
  44.     , D1.Operator
  45.     , D1.Casy_type
  46.     , D1.Sub_type
  47.     , D1.Sub_3
  48.     , D1.result
  49.     , D1.num_drive
  50. INTO #olp_dialog_report
  51. FROM #dialogtabletemp D1
  52. LEFT JOIN #dialogtabletemp as D2
  53.     ON D1.IdChain = D2.IdChain
  54.  
  55. DROP TABLE #dialogtabletemp
  56.  
  57. DECLARE @final TABLE
  58.     (
  59.         idchain uniqueidentifier,datewrite datetime ,callerid nvarchar(50)
  60.         ,idtask uniqueidentifier,operator nvarchar(100),casy_type nvarchar(max)
  61.         ,sub_type nvarchar(max),sub_3 nvarchar(max),mark int,post int, result int, num_drive int
  62.     )
  63. INSERT INTO @final
  64. SELECT DISTINCT od1.idchain
  65.     , OD2.DateWrite
  66.     , OD2.CallerID
  67.     , OD2.idtask
  68.     , OD2.Operator
  69.     , OD2.Casy_type
  70.     , OD2.Sub_type
  71.     , OD2.Sub_3
  72.     , TM.MARK
  73.     , NULL
  74.     , OD2.result
  75.     , OD2.num_drive
  76. FROM #olp_dialog_report as OD1
  77. LEFT JOIN oktell.dbo.A_Users au
  78.     ON au.name=OD1.Operator
  79. LEFT JOIN #olp_dialog_report as OD2
  80.     ON OD1.IdChain = OD2.IdChain
  81. LEFT JOIN oktell.dbo.tbl_MARK as TM
  82.     ON TM.IDHCAIN = OD1.IdChain
  83. LEFT JOIN oktell_cc_temp.dbo.A_Cube_CC_EffortConnections eff with (nolock)
  84.     ON eff.IdChain=OD1.IdChain
  85. WHERE OD1.DateWrite BETWEEN @DateStart AND @DateEnd
  86.     AND CAST(cast(OD1.DateWrite as time)AS datetime) BETWEEN @TimeStart AND @TimeEnd
  87.     AND OD1.idtask IN (SELECT id FROM @tasks)
  88.     AND eff.IdOperator IN (SELECT id FROM @opers)
  89.     AND au.ID IN (SELECT id FROM @opers)
  90.  
  91. DROP TABLE #olp_dialog_report
  92.  
  93. UPDATE @final
  94. SET post = (
  95. case
  96.     when
  97.     (
  98.         select sum(lentime)
  99.         from oktell_cc_temp.[dbo].[A_Cube_CC_OperatorStates]
  100.         where state=7 and idoperator=au.ID
  101.             and ideffort in
  102.                 (
  103.                     SELECT DISTINCT ideffort FROM oktell_cc_temp.dbo.A_Cube_CC_EffortConnections WHERE idchain=t.idchain
  104.                 )
  105.     )>41
  106.     then
  107.         (
  108.             select datediff(SECOND,(select top 1 datetimestop from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with (nolock) where IdOperator = au.ID and idchain=t.idchain order by datetimestop desc),(select max(DateTimeStop) from oktell_cc_temp.[dbo].[A_Cube_CC_OperatorStates] where state=7 and idoperator=au.ID and ideffort in(select distinct ideffort from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections where idchain=t.idchain)))
  109.         )
  110.     else
  111.         (
  112.             select sum(lentime)
  113.             from oktell_cc_temp.[dbo].[A_Cube_CC_OperatorStates]
  114.             where state=7 and idoperator=au.ID
  115.                 and ideffort in
  116.                     (
  117.                         select distinct ideffort from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections where idchain=t.idchain
  118.                     )
  119.         )
  120.     end
  121. )
  122. FROM @final t
  123. LEFT JOIN oktell.dbo.a_users au
  124.     ON au.Name = t.operator
  125.  
  126. SELECT datewrite [Дата, Время]
  127.     , callerid[Номер абонента]
  128.     , tsk.Name[Задача]
  129.     , operator[Оператор]
  130.     , casy_type
  131.     , sub_type
  132.     , sub_3
  133.     , mark[Оценка]
  134.     , CAST(DATEADD(ss,post,'00:00') as time(0)) post
  135.     , IIF(result != -1, result, NULL) [Оценка Оператором]
  136.     , IIF(num_drive != -1, result, NULL) [Номер поездки]
  137.     ,'http://gett.oktell.olp.site:4055/download/byscript?name=Test_voice&startparam1='+CAST(idchain as nvarchar(50))+'&async=0&timeout=10' [Ссылка на запись]
  138. INTO #result_temp
  139. FROM @final as od
  140. LEFT JOIN [oktell].dbo.[A_TaskManager_Tasks] as tsk
  141.     ON tsk.Id = od.idtask
  142.  
  143. DELETE @final
  144.  
  145.  
  146. IF(@type=1 AND (@AbonentNumber='' OR @AbonentNumber IS NULL))
  147. BEGIN
  148.     SELECT * FROM #result_temp
  149.     ORDER BY [Оператор]
  150. END
  151. IF((@type=0 or @type is null) and ( @AbonentNumber='' OR @AbonentNumber IS NULL))
  152. BEGIN  
  153.     SELECT * FROM #result_temp
  154.     WHERE casy_type IS NULL OR casy_type=''
  155.     ORDER BY [Дата, Время]
  156. END
  157. IF((@type=0 OR @type IS NULL) AND ( @AbonentNumber<>'' OR @AbonentNumber IS NOT NULL))
  158. BEGIN
  159.     SELECT * FROM #result_temp
  160.     WHERE (casy_type IS NULL OR casy_type='' ) AND [Номер абонента] LIKE '%'+@AbonentNumber+'%'
  161.     ORDER BY [Дата, Время]
  162. END
  163. IF(@type=1 AND ( @AbonentNumber<>'' OR @AbonentNumber IS NOT NULL))
  164. begin
  165.     SELECT * FROM #result_temp
  166.     where [Номер абонента] LIKE '%'+@AbonentNumber+'%'
  167. end
  168.  
  169. DROP TABLE #result_temp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement