create proc UtilizationRateByHalfHour(@searchdatechar varchar(24),@CityCode varchar(10)) as begin declare @begintime datetime declare @endtime datetime declare @orgId varchar(40) declare @logintime_hour00 int, @logintime_hour01 int, @logintime_hour02 int, @logintime_hour03 int, @logintime_hour04 int, @logintime_hour05 int, @logintime_hour06 int, @logintime_hour07 int, @logintime_hour08 int, @logintime_hour09 int, @logintime_hour10 int, @logintime_hour11 int, @logintime_hour12 int, @logintime_hour13 int, @logintime_hour14 int, @logintime_hour15 int, @logintime_hour16 int, @logintime_hour17 int, @logintime_hour18 int, @logintime_hour19 int, @logintime_hour20 int, @logintime_hour21 int, @logintime_hour22 int, @logintime_hour23 int, @logintime_hour24 int, @logintime_hour25 int, @logintime_hour26 int, @logintime_hour27 int, @logintime_hour28 int, @logintime_hour29 int, @logintime_hour30 int, @logintime_hour31 int, @logintime_hour32 int, @logintime_hour33 int, @logintime_hour34 int, @logintime_hour35 int, @logintime_hour36 int, @logintime_hour37 int, @logintime_hour38 int, @logintime_hour39 int, @logintime_hour40 int, @logintime_hour41 int, @logintime_hour42 int, @logintime_hour43 int, @logintime_hour44 int, @logintime_hour45 int, @logintime_hour46 int, @logintime_hour47 int declare @acwtime_hour00 int, @acwtime_hour01 int, @acwtime_hour02 int, @acwtime_hour03 int, @acwtime_hour04 int, @acwtime_hour05 int, @acwtime_hour06 int, @acwtime_hour07 int, @acwtime_hour08 int, @acwtime_hour09 int, @acwtime_hour10 int, @acwtime_hour11 int, @acwtime_hour12 int, @acwtime_hour13 int, @acwtime_hour14 int, @acwtime_hour15 int, @acwtime_hour16 int, @acwtime_hour17 int, @acwtime_hour18 int, @acwtime_hour19 int, @acwtime_hour20 int, @acwtime_hour21 int, @acwtime_hour22 int, @acwtime_hour23 int, @acwtime_hour24 int, @acwtime_hour25 int, @acwtime_hour26 int, @acwtime_hour27 int, @acwtime_hour28 int, @acwtime_hour29 int, @acwtime_hour30 int, @acwtime_hour31 int, @acwtime_hour32 int, @acwtime_hour33 int, @acwtime_hour34 int, @acwtime_hour35 int, @acwtime_hour36 int, @acwtime_hour37 int, @acwtime_hour38 int, @acwtime_hour39 int, @acwtime_hour40 int, @acwtime_hour41 int, @acwtime_hour42 int, @acwtime_hour43 int, @acwtime_hour44 int, @acwtime_hour45 int, @acwtime_hour46 int, @acwtime_hour47 int declare @acdtime_hour00 int, @acdtime_hour01 int, @acdtime_hour02 int, @acdtime_hour03 int, @acdtime_hour04 int, @acdtime_hour05 int, @acdtime_hour06 int, @acdtime_hour07 int, @acdtime_hour08 int, @acdtime_hour09 int, @acdtime_hour10 int, @acdtime_hour11 int, @acdtime_hour12 int, @acdtime_hour13 int, @acdtime_hour14 int, @acdtime_hour15 int, @acdtime_hour16 int, @acdtime_hour17 int, @acdtime_hour18 int, @acdtime_hour19 int, @acdtime_hour20 int, @acdtime_hour21 int, @acdtime_hour22 int, @acdtime_hour23 int, @acdtime_hour24 int, @acdtime_hour25 int, @acdtime_hour26 int, @acdtime_hour27 int, @acdtime_hour28 int, @acdtime_hour29 int, @acdtime_hour30 int, @acdtime_hour31 int, @acdtime_hour32 int, @acdtime_hour33 int, @acdtime_hour34 int, @acdtime_hour35 int, @acdtime_hour36 int, @acdtime_hour37 int, @acdtime_hour38 int, @acdtime_hour39 int, @acdtime_hour40 int, @acdtime_hour41 int, @acdtime_hour42 int, @acdtime_hour43 int, @acdtime_hour44 int, @acdtime_hour45 int, @acdtime_hour46 int, @acdtime_hour47 int declare @ihour int set @begintime=convert(datetime,@searchdatechar) set @endtime=DATEADD(day,1,@begintime) --set @begintime='2022-05-09' --set @endtime='2022-05-10' set @orgId='b05d5ac9-82b6-4abe-b343-36ddc94672d1' if (@CityCode='SZ') set @orgId='2b568031-39a1-4117-9927-39b7ade19e66' if (@CityCode='JN') set @orgId='f454f95c-9ab2-4499-936f-e746195c7487' if OBJECT_ID('tempdb..#t_AgentLoginLog') is not null drop table #t_AgentLoginLog if OBJECT_ID('tempdb..#t_iagent') is not null drop table #t_iagent if OBJECT_ID('tempdb..#t_stationcallrecord') is not null drop table #t_stationcallrecord select * into #t_AgentLoginLog from AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < @endtime) or (LogoutTime >=@begintime and LogoutTime < @endtime) or (LoginTime >=@begintime and LoginTime < @endtime and LogoutTime is null) ) and AgentID in ( select distinct Agent.Agent from Agent with(nolock) join Org_Agent with(nolock) on Agent.AgentID=Org_Agent.AgentID join Organization with(nolock) on Org_Agent.OrgID=Organization.OrgID and Org_Agent.OrgID in (@orgId) ) ; --某天每个小时的登录总时长 set @ihour=1; select @logintime_hour00 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour01 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour02 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour03 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour04 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour05 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour06 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour07 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour08 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour09 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour10 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour11 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour12 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour13 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour14 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour15 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour16 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour17 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour18 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour19 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour20 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour21 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour22 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour23 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour24 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour25 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour26 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour27 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour28 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour29 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour30 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour31 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour32 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour33 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour34 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour35 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour36 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour37 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour38 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour39 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour40 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour41 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour42 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour43 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour44 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour45 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour46 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; set @ihour=@ihour+1; select @logintime_hour47 = ISNULL(sum(DATEDIFF(ss, case when LoginTime<@begintime then CONVERT(datetime,@begintime) else LoginTime end, case when LogoutTime>dateadd(MINUTE,@ihour*30,@begintime) then dateadd(MINUTE,@ihour*30,@begintime) when LogoutTime is null then dateadd(MINUTE,@ihour*30,@begintime) else LogoutTime end )),0) from #t_AgentLoginLog where ( (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LogoutTime >=@begintime and LogoutTime < dateadd(MINUTE,@ihour*30,@begintime)) or (LoginTime >=@begintime and LoginTime < dateadd(MINUTE,@ihour*30,@begintime) and LogoutTime is null) ) ; --每小时总的话后总时长 select * into #t_iagent from iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)) and logid in ( select distinct Agent.Agent from Agent with(nolock) join Org_Agent with(nolock) on Agent.AgentID=Org_Agent.AgentID join Organization with(nolock) on Org_Agent.OrgID=Organization.OrgID and Org_Agent.OrgID in (@orgId) ) set @ihour=1; select @acwtime_hour00= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour01= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour02= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour03= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour04= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour05= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour06= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour07= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour08= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour09= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour10= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour11= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour12= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour13= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour14= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour15= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour16= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour17= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour18= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour19= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour20= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour21= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour22= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour23= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour24= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour25= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour26= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour27= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour28= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour29= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour30= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour31= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour32= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour33= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour34= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour35= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour36= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour37= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour38= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour39= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour40= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour41= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour42= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour43= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour44= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour45= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour46= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); set @ihour=@ihour+1; select @acwtime_hour47= isnull(sum(i_acwtime),0) from #t_iagent where 1=1 and (row_date >=@begintime and row_date < dateadd(MINUTE,@ihour*30,@begintime)); --每小时总的通话总时长 select * into #t_stationcallrecord from stationcallrecord with(nolock) where 1=1 and (End_Time >=@begintime and End_Time <@endtime) 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') and calldirect=1 and split != '' and AgentID in ( select distinct Agent.Agent from Agent with(nolock) join Org_Agent with(nolock) on Agent.AgentID=Org_Agent.AgentID join Organization with(nolock) on Org_Agent.OrgID=Organization.OrgID and Org_Agent.OrgID in (@orgId) ); set @ihour=1; select @acdtime_hour00= isnull(sum(talk_dur),0) from #t_stationcallrecord where 1=1 and (End_Time >=@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time =@begintime and End_Time