2022051203-UtilizationRateByTime.sql 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. create proc UtilizationRateByTime(@searchdatechar varchar(24),@CityCode varchar(10)) as
  2. begin
  3. declare @begintime datetime
  4. declare @endtime datetime
  5. declare @orgId varchar(40)
  6. declare @logintime_hour00 int
  7. declare @acwtime_hour00 int
  8. declare @acdtime_hour00 int
  9. declare
  10. @i_acwtime1 int,
  11. @i_acwtime2 int,
  12. @i_acwtime3 int,
  13. @i_acwtime4 int
  14. declare
  15. @i_stafftime1 int,
  16. @i_stafftime2 int,
  17. @i_stafftime3 int,
  18. @i_stafftime4 int
  19. --开始时间,选择当天的00:00:00
  20. set @begintime=convert(datetime,@searchdatechar)
  21. --结束时间,下一天的00:00:00
  22. set @endtime=DATEADD(day,1,@begintime)
  23. --结束日期比当前时间大
  24. if @endtime>=GETDATE()
  25. set @endtime=getdate()
  26. --set @begintime='2022-05-09'
  27. --set @endtime='2022-05-10'
  28. set @orgId='b05d5ac9-82b6-4abe-b343-36ddc94672d1'
  29. if (@CityCode='SZ')
  30. set @orgId='2b568031-39a1-4117-9927-39b7ade19e66'
  31. if (@CityCode='JN')
  32. set @orgId='f454f95c-9ab2-4499-936f-e746195c7487'
  33. if OBJECT_ID('tempdb..#compute_agentloginlog') is not null
  34. drop table #compute_agentloginlog
  35. if OBJECT_ID('tempdb..#compute_agentstatelog') is not null
  36. drop table #compute_agentstatelog
  37. if OBJECT_ID('tempdb..#t_stationcallrecord') is not null
  38. drop table #t_stationcallrecord
  39. if OBJECT_ID('tempdb..#SearchAgent') is not null
  40. drop table #SearchAgent
  41. select distinct Agent.Agent into #SearchAgent from Agent with(nolock) join Org_Agent with(nolock) on
  42. Agent.AgentID=Org_Agent.AgentID join Organization with(nolock) on Org_Agent.OrgID=Organization.OrgID
  43. and Org_Agent.OrgID in (@orgId)
  44. --某天每个小时的登录总时长
  45. select * into #compute_agentloginlog from agentloginlog
  46. where 1=1
  47. and (
  48. logintime >= @begintime and logintime < @endtime and logouttime >= @begintime and logouttime < @endtime
  49. or (logintime <= @begintime and (logouttime >= @endtime or LogoutTime is null))
  50. or (logintime <= @begintime and logouttime >= @begintime and logouttime < @endtime)
  51. or (logintime >= @begintime and logintime < @endtime and (logouttime > @endtime or LogoutTime is null))
  52. )
  53. and agentid in (select agent from #SearchAgent)
  54. --登入时间和登出时间都在@row_date_a和@row_date_b之间,此时计算logouttime-logintime=?
  55. select @i_stafftime1=isnull(sum(isnull(datediff(ss,logintime, logouttime),0)),0)
  56. from #compute_agentloginlog
  57. where
  58. logintime >= @begintime and logintime < @endtime and logouttime >= @begintime and logouttime < @endtime
  59. --登入时间在@row_date_a之前,登出时间在@row_date_b之后或者没有登出时间,此时计算@intrvl*3600秒=?
  60. select @i_stafftime2=isnull(sum(isnull(datediff(ss,@begintime, @endtime),0)),0)
  61. from #compute_agentloginlog
  62. where
  63. logintime <= @begintime and (logouttime >= @endtime or LogoutTime is null)
  64. --登入时间在@row_date_a之前,登出时间在@row_date_a和@row_date_b之间,此时计算logouttime-@row_date_a=?
  65. select @i_stafftime3=isnull(sum(isnull(datediff(ss,@begintime, logouttime),0)),0)
  66. from #compute_agentloginlog
  67. where
  68. logintime <= @begintime and logouttime >= @begintime and logouttime < @endtime
  69. --登入时间在@row_date_a和@row_date_b之间,登出时间在@row_date_b之后或者没有登出时间
  70. select @i_stafftime4=isnull(sum(isnull(datediff(ss,logintime, @endtime),0)),0)
  71. from #compute_agentloginlog
  72. where
  73. logintime >= @begintime and logintime < @endtime and (logouttime > @endtime or LogoutTime is null)
  74. set @logintime_hour00=@i_stafftime1+@i_stafftime2+@i_stafftime3+@i_stafftime4
  75. --每小时总的话后总时长
  76. select * into #compute_agentstatelog from agentstatelog
  77. where 1=1 and state=2
  78. and (
  79. (begintime >= @begintime and begintime < @endtime and endtime >= @begintime and endtime < @endtime )
  80. or (begintime <= @begintime and (Endtime >= @endtime or Endtime is null))
  81. or (begintime < @begintime and endtime >= @begintime and endtime < @endtime)
  82. or (begintime >= @begintime and begintime < @endtime and (Endtime > @endtime or Endtime is null))
  83. )
  84. and agentid in (select Agent from #SearchAgent)
  85. select @i_acwtime1=isnull(sum(isnull(datediff(ss,begintime, endtime),0)),0) from #compute_agentstatelog
  86. where
  87. begintime >= @begintime and begintime < @endtime and endtime >= @begintime and endtime < @endtime
  88. select @i_acwtime2=isnull(sum(isnull(datediff(ss,@begintime, @endtime),0)),0) from #compute_agentstatelog
  89. where
  90. begintime <= @begintime and (Endtime >= @endtime or Endtime is null)
  91. select @i_acwtime3=isnull(sum(isnull(datediff(ss,@begintime, endtime),0)),0) from #compute_agentstatelog
  92. where
  93. begintime < @begintime and endtime >= @begintime and endtime < @endtime
  94. select @i_acwtime4=isnull(sum(isnull(datediff(ss,begintime, @endtime),0)),0) from #compute_agentstatelog
  95. where
  96. begintime >= @begintime and begintime < @endtime and (Endtime > @endtime or Endtime is null)
  97. set @acwtime_hour00=@i_acwtime1+@i_acwtime2+@i_acwtime3+@i_acwtime4
  98. --每小时总的通话总时长
  99. select * into #t_stationcallrecord
  100. from stationcallrecord with(nolock)
  101. where 1=1
  102. and (End_Time >=@begintime and End_Time <@endtime)
  103. --and split in ('841001','841002','841009','841012','841013','842030','841041','841301','841302','841303','841304','841305','841306','841307','841311','841312','841313','841314','841315','841316','841317','841341','841342','841343','841344','841345','841346','841347','841321','841322','841323','841324','841325','841326','841327','841331','841332','841333','841334','841335','841336','841337','841351','841352','841353','841354','841355','841356','841357')
  104. and calldirect=1
  105. and split != ''
  106. and AgentID in
  107. (
  108. select Agent from #SearchAgent
  109. );
  110. select @acdtime_hour00=
  111. isnull(sum(talk_dur),0)
  112. from #t_stationcallrecord
  113. where 1=1 and (End_Time >=@begintime and End_Time <@endtime)
  114. ;
  115. select case when @logintime_hour00=0 then 0 else round(cast( (@acdtime_hour00+@acwtime_hour00) as float)/@logintime_hour00,4) end as UtilizationRate00
  116. if OBJECT_ID('tempdb..#compute_agentloginlog') is not null
  117. drop table #compute_agentloginlog
  118. if OBJECT_ID('tempdb..#compute_agentstatelog') is not null
  119. drop table #compute_agentstatelog
  120. if OBJECT_ID('tempdb..#t_stationcallrecord') is not null
  121. drop table #t_stationcallrecord
  122. if OBJECT_ID('tempdb..#SearchAgent') is not null
  123. drop table #SearchAgent
  124. end