In Columns:
SUM(CASE WHEN cqd.queuetime < AVG(cqd.queuetime) THEN 1 ELSE 0 END) as 'Qtde < Média' e
SUM(CASE WHEN cqd.queuetime > AVG(cqd.queuetime) THEN 1 ELSE 0 END) as 'Qtde > Média',
returns the following error in SQL SERVER 2018:
Can not perform an aggregate function on an expression containing an aggregate or a subquery.
What am I doing wrong?
Thank you!
Full script:
select csq.csqname as Cqs, CONVERT(TIME,ccd.startdatetime,10) "Time", r.resourcename AS Agente,
SUM(CASE WHEN cqd.queuetime > 20 THEN 1 ELSE 0 END) as 'Total(>20)',
MAX(cqd.queuetime) as '> Número',
MAX(ccd.connecttime) as '>Tempo',
SUM(CASE WHEN asd.eventtype = 1 THEN 1 ELSE 0 END) as 'Logados',
SUM(CASE WHEN asd.eventtype = 5 THEN 1 ELSE 0 END) as 'Em Atendimento',
--qtde < média qdt qtd duração de chamadas < média
SUM(CASE WHEN cqd.queuetime < AVG(cqd.queuetime) THEN 1 ELSE 0 END) as 'Qtde > Média',
MIN(cqd.queuetime) as 'Menor tempo de ligação',
AVG(cqd.queuetime) as 'Tempo médio de atendimento',
STDEVP(cqd.queuetime) as 'Desvio Padrão',
MAX(cqd.queuetime) as 'Maior tempo de ligação',
--qtde > média qtd duração de chamadas > média
SUM(CASE WHEN cqd.queuetime > AVG(cqd.queuetime) THEN 1 ELSE 0 END) as 'Qtde > Média',
SUM(CASE WHEN cqd.disposition = 2 THEN 1 ELSE 0 END) as 'Atendidas',
SUM(CASE WHEN cqd.disposition = 1 THEN 1 ELSE 0 END) as 'Abandonadas'
from [resource] r
INNER JOIN teamcsqmapping on r.assignedteamid = teamcsqmapping.teamid
INNER JOIN contactservicequeue csq on teamcsqmapping.csqid = csq.contactservicequeueid
INNER JOIN contactcalldetail ccd on r.resourceid = ccd.destinationID and ccd.destinationtype = 1
JOIN contactqueuedetail cqd on csq.contactservicequeueid = cqd.targetid and cqd.targettype = 0
INNER JOIN agentstatedetail asd on r.resourceid = asd.agentid
WHERE
CONVERT(TIME,ccd.startdatetime,10) BETWEEN '14:00' AND '14:59'
GROUP BY csqname, CONVERT(TIME,ccd.startdatetime,10), r.resourcename
ORDER BY resourcename, 2 ASC