0
In the 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:
Cannot 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
Could you send me an example, please?
– Thiago Simão
@Thiagosimão I will assemble a simplified example, since we do not have the structure of its tables
– Sorack
Thank you very much!
– Thiago Simão
Using OVER one can compare line by line with the médiacfor example https://docs.microsoft.com/pt-br/sql/t-sql/queries/select-over-clause-transact-sql?view=sql-server-2017
– Motta