Advertisement
WorkAkkaunt

Gett Taxi My

Jul 2nd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 9.46 KB | None | 0 0
  1. declare @type int = 0
  2.  
  3. declare @DateStart datetime;
  4. declare @DateEnd datetime;
  5. declare @TimeStart datetime;
  6. declare @TimeEnd datetime;
  7.  
  8. set @DateStart = '20180821 00:00:00.000'
  9. set @DateEnd = '20180821 23:59:00.000'
  10. set @TimeStart = '19000101 00:00:00.000'
  11. set @TimeEnd = '19000101 23:59:00.000'
  12. declare @tasks table (id uniqueidentifier)
  13. declare @opers table ( id uniqueidentifier)
  14. insert into @opers
  15. select distinct id from oktell.dbo.A_Users
  16. insert into @tasks
  17. select id from oktell.dbo.A_TaskManager_Tasks where name ='B2D Premium'
  18.  
  19. --- не очень хорошее решение в отношении выпадающего списка. Будет тормозить.
  20. declare @TypeClient table(id nvarchar(250), name nvarchar(250));
  21. insert into @TypeClient
  22. select distinct TypeClient, TypeClient from oktell..Olp_dialog
  23.  
  24. declare @Status table(id nvarchar(250), name nvarchar(250))
  25. insert into @Status
  26. select distinct Status, Status from oktell..Olp_dialog
  27.  
  28. declare @Solution table(id nvarchar(250), name nvarchar(250))
  29. insert into @Solution
  30. select distinct Solution, Solution from oktell..Olp_dialog
  31.  
  32. declare @subjct2 int =1
  33.  
  34. declare @Other nvarchar(250);
  35. set @Other = ''
  36.  
  37. declare @AbonentNumber nvarchar(250);
  38. set @AbonentNumber = ''
  39.  
  40. --@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  41. declare @olp_dialog table (idchain uniqueidentifier, datewrite datetime ,callerid nvarchar(50),idtask uniqueidentifier,operator nvarchar(100),casy_type nvarchar(max),sub_type nvarchar(max),sub_3 nvarchar(max))
  42.    
  43. --insert into @olp_dialog
  44. select od.IdChain,datewrite, callerID,od.idtask,Operator,Casy_type,Sub_type,Sub_3
  45. into #dialogtabletemp
  46. from oktell..Olp_dialog od
  47. --left join oktell.dbo.A_Users au on au.name=od.Operator Старый джойн, возможно пригодится, заменен на eff
  48. left join oktell_cc_temp.dbo.A_Cube_CC_EffortConnections eff with (nolock) on eff.IdChain=od.IdChain
  49. where od.DateWrite between @DateStart and @DateEnd
  50.     and CAST(cast(od.DateWrite as time)AS datetime) between @TimeStart and @TimeEnd
  51.     and od.idtask in (select id from @tasks)
  52.     --and au.ID in (select id from @opers)
  53.     and eff.IdOperator in (select id from @opers)
  54.     and CallResult !=6 -- строка добавлена по AI-11600 14.11.2018, раньше было без нее
  55.  
  56. --insert into @olp_dialog
  57. select distinct IdChain
  58.     ,(select top 1 DateWrite from #dialogtabletemp dd2 where dd2.IdChain=dd1.IdChain order by DateWrite) DateWrite
  59.     ,CallerID
  60.     ,(select top 1 idtask from #dialogtabletemp dd2 where dd2.IdChain=dd1.IdChain order by DateWrite desc) idtask
  61.     ,Operator
  62.     ,Casy_type
  63.     ,Sub_type
  64.     ,Sub_3
  65. into #olp_dialog_report
  66. from #dialogtabletemp dd1
  67.  
  68. drop table #dialogtabletemp
  69.  
  70. declare @final table (idchain uniqueidentifier,datewrite datetime ,callerid nvarchar(50),idtask uniqueidentifier,operator nvarchar(100),casy_type nvarchar(max),sub_type nvarchar(max),sub_3 nvarchar(max),mark int,post int)
  71. insert into @final
  72. select distinct od.idchain
  73.     ,isnull((select top 1  datewrite from #olp_dialog_report where IdChain=od.IdChain order by datewrite asc),(select top 1  datewrite from #olp_dialog_report where IdChain=od.IdChain order by datewrite desc))
  74.     ,isnull((select top 1  CallerID from #olp_dialog_report where IdChain=od.IdChain order by datewrite asc),(select top 1  CallerID from #olp_dialog_report where IdChain=od.IdChain order by datewrite desc))
  75.     ,isnull((select top 1  idtask from #olp_dialog_report where IdChain=od.IdChain order by datewrite asc),(select top 1  idtask from #olp_dialog_report where IdChain=od.IdChain order by datewrite desc))
  76.     ,isnull((select top 1  Operator from #olp_dialog_report where IdChain=od.IdChain order by datewrite asc ),(select top 1  Operator from #olp_dialog_report where IdChain=od.IdChain order by datewrite desc ))
  77.     ,isnull((select top 1  Casy_type from #olp_dialog_report where IdChain=od.IdChain order by datewrite asc),(select top 1  Casy_type from #olp_dialog_report where IdChain=od.IdChain order by datewrite desc))
  78.     ,isnull((select top 1  Sub_type from #olp_dialog_report where IdChain=od.IdChain order by datewrite asc),(select top 1  Sub_type from #olp_dialog_report where IdChain=od.IdChain order by datewrite desc))
  79.     ,isnull((select top 1  Sub_3 from #olp_dialog_report where IdChain=od.IdChain order by datewrite asc),(select top 1  Sub_3 from #olp_dialog_report where IdChain=od.IdChain order by datewrite desc))
  80.     ,(select  top 1 mark from oktell.dbo.tbl_MARK where idhcain=od.IdChain)[Mark]
  81.     ,null
  82.     --case
  83.     --  when
  84.     --  (
  85.     --  select sum(lentime) from oktell_cc_temp.[dbo].[A_Cube_CC_OperatorStates] where state=7 and idoperator=eff.IdOperator and ideffort in(select distinct ideffort from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections where idchain=od.IdChain)
  86.     --  )>41
  87.     --then
  88.     --  (select datediff(SECOND,(select top 1 datetimestop from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections with (nolock) where IdOperator = eff.IdOperator and idchain=od.IdChain order by datetimestop desc),(select max(DateTimeStop) from oktell_cc_temp.[dbo].[A_Cube_CC_OperatorStates] where state=7 and idoperator=eff.IdOperator and ideffort in(select distinct ideffort from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections where idchain=od.IdChain))))
  89.     --else
  90.     --  (
  91.     --  select sum(lentime) from oktell_cc_temp.[dbo].[A_Cube_CC_OperatorStates] where state=7 and idoperator=eff.IdOperator and ideffort in(select distinct ideffort from oktell_cc_temp.dbo.A_Cube_CC_EffortConnections where idchain=od.IdChain)
  92.     --  )
  93.     --end
  94.    
  95. from #olp_dialog_report od
  96.     left join oktell.dbo.A_Users au on au.name=od.Operator
  97.     --left join oktell_cc_temp.dbo.A_Cube_CC_EffortConnections eff with (nolock) on eff.IdChain=od.IdChain 
  98.     where od.DateWrite between @DateStart and @DateEnd
  99.     and CAST(cast(od.DateWrite as time)AS datetime) between @TimeStart and @TimeEnd
  100.     and od.idtask in (select id from @tasks)
  101.     --and eff.IdOperator in (select id from @opers)
  102.     and au.ID in (select id from @opers)
  103.  
  104. drop table #olp_dialog_report
  105.  
  106.  
  107. update @final
  108. set post = (
  109. case
  110.             when
  111.             (
  112.             select sum(lentime) 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)
  113.             )>41
  114.         then
  115.             (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))))
  116.         else
  117.             (
  118.             select sum(lentime) 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)
  119.             )
  120.         end
  121. )
  122. from @final t
  123. left join oktell.dbo.a_users au on au.Name = t.operator
  124.  
  125.  
  126. DECLARE @ResultTable TABLE
  127.     (
  128.         Id uniqueidentifier,
  129.         datewrite datetime, CallerId nvarchar(250), IdTask uniqueidentifier,
  130.         [Name] nvarchar(250), Operator nvarchar(250)
  131.         ,casy_type nvarchar(250), SubType nvarchar(250), Sub3 nvarchar(250), Mark int
  132.         ,[Time] smalldatetime, Link nvarchar(250)
  133.     )
  134.  
  135. INSERT INTO @ResultTable
  136. SELECT idchain, datewrite [Дата, Время]
  137.     ,callerid[Номер абонента]
  138.     ,idtask
  139.     ,tsk.Name[Задача]
  140.     ,operator[Оператор]
  141.     ,casy_type
  142.     ,sub_type
  143.     ,sub_3
  144.     ,mark[Оценка]
  145.     ,cast(dateadd(ss,post,'00:00')  as time(0)) post
  146.     ,'http://gett.oktell.olp.site:4055/download/byscript?name=Test_voice&startparam1='+cast(idchain as nvarchar(50))+'&async=0&timeout=10' [Ссылка на запись]
  147. FROM @final od
  148. LEFT JOIN oktell.dbo.A_TaskManager_Tasks tsk ON tsk.id=od.idtask
  149.  
  150. DECLARE @ResultTable2 TABLE
  151.     (
  152.         Id uniqueidentifier,
  153.         datewrite datetime, CallerId nvarchar(250), IdTask uniqueidentifier,
  154.         [Name] nvarchar(250), Operator nvarchar(250)
  155.         ,casy_type nvarchar(250), SubType nvarchar(250), Sub3 nvarchar(250), Mark int
  156.         ,[Time] smalldatetime, Link nvarchar(250), OperMark int
  157.     )
  158.  
  159. INSERT INTO @ResultTable2
  160. SELECT *
  161.     ,(SELECT TOP 1 result FROM [oktell].[dbo].[Olp_dialog] od WHERE od.IdChain = [@ResultTable].Id)
  162. FROM @ResultTable
  163.  
  164. --select * from @ResultTable2
  165.  
  166. IF (@type = 1 AND (@AbonentNumber='' or @AbonentNumber is null))
  167. BEGIN
  168.     SELECT datewrite, CallerId ,[Name] ,Operator ,casy_type ,SubType ,Sub3 ,Mark ,[Time] ,Link ,OperMark
  169.     FROM @ResultTable2
  170.     ORDER BY Operator
  171. END
  172.  
  173. IF (@type = 0 AND (@AbonentNumber='' or @AbonentNumber is null))
  174. BEGIN
  175.     SELECT datewrite, CallerId ,[Name] ,Operator ,casy_type ,SubType ,Sub3 ,Mark ,[Time] ,Link ,OperMark
  176.     FROM @ResultTable2
  177.     where casy_type is null or casy_type=''
  178.     order by datewrite
  179. END
  180.  
  181. IF (@type=0 and ( @AbonentNumber<>'' or @AbonentNumber is not null))
  182. BEGIN
  183.     SELECT datewrite, CallerId ,[Name] ,Operator ,casy_type ,SubType ,Sub3 ,Mark ,[Time] ,Link ,OperMark
  184.     FROM @ResultTable2
  185.     WHERE (casy_type is null or casy_type='' ) and callerid like '%'+@AbonentNumber+'%'
  186.     ORDER BY datewrite
  187. END
  188.  
  189. IF (@type = 1 AND (@AbonentNumber='' or @AbonentNumber is null))
  190. BEGIN
  191.     SELECT datewrite, CallerId ,[Name] ,Operator ,casy_type ,SubType ,Sub3 ,Mark ,[Time] ,Link ,OperMark
  192.     FROM @ResultTable2
  193.     WHERE callerid LIKE '%'+@AbonentNumber+'%'
  194. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement