using sample data, others might have a better solution. at least we now have sample data.
use SQLTeam
go
create table #acgcustomcalllog
(
lineofbusiness varchar(100),
interactionidkey int,
datetimeadded datetime
)
go
/*
*/
insert into #acgcustomcalllog(lineofbusiness, interactionidkey, datetimeadded)
select 'Robot Recycling', 1, dateadd(hour,2^1,getdate()) union
select 'Robot Recycling', 2, dateadd(hour,2^2,getdate()) union
select 'Robot Recycling', 3, dateadd(hour,2^3,getdate()) union
select 'Robot Recycling', 4, dateadd(hour,2^4,getdate()) union
select 'Robot Recycling', 5, dateadd(hour,2^5,getdate()) union
select 'Robot Recycling', 6, dateadd(hour,2^6,getdate())
create table #interactionsummary
(
remotenumberfmt varchar(100),
interactionidkey int,
lastlocaluserID int ,
lastlocalname varchar(100)
)
go
--select * from #acgcustomcalllog
insert into #interactionsummary(remotenumberfmt, interactionidkey, lastlocalname, lastlocaluserID)
select '104-104-' + cast(interactionidkey as varchar(150)), interactionidkey, 'Darth Agent', 1 from #acgcustomcalllog
select _24hours.lastlocalname, _24hours._hoursWorked ,
th._hoursCallingOneNumber,
( (th._hoursCallingOneNumber*1.00)/ (_24hours._hoursWorked*1.00) ) *100 _percentage,
remotenumberfmt
from (
select lastlocalname, sum(DateDiff(hour, getdate(), datetimeadded)) _hoursWorked
from #acgcustomcalllog a
join #interactionsummary b on a.interactionidkey = b.interactionidkey
where DateDiff(hour, getdate(), datetimeadded) between 1 and 24
group by lastlocalname
) _24hours
left join (
select sum(DateDiff(hour, getdate(), datetimeadded)) _hoursCallingOneNumber, lastlocalname, remotenumberfmt
from #acgcustomcalllog a
join #interactionsummary b on a.interactionidkey = b.interactionidkey
where DateDiff(hour, getdate(), datetimeadded) between 1 and 24
group by remotenumberfmt, lastlocalname
) th on _24hours.lastlocalname = th.lastlocalname
--join (select distinct lastlocalname, remotenumberfmt from #interactionsummary) telno on telno.lastlocalname = th.lastlocalname
drop table #interactionsummary
drop table #acgcustomcalllog