Usuário com melhor resposta
Ajuda com Query

Pergunta
-
Boa tarde
Galera!
Estou criando uma consulta para gerar um relatório e preciso de uma ajuda, a consulta está da seguinte forma e preciso trazer A quantidade por mês Exemplo:
Motivo Jan Fev Mar TESTE 1 2 3 5 TESTE 2 5 6 Obs.
Quando rodo a consulta recebo a mensagem de erro abaixo.
Mensagem 130, Nível 15, Estado 1, Linha 7
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
quem puder ajudar agradeço.
Obrigado.
Select
O.MotOcorCodEstr,
Count(O.MotOcorCodEstr) AS QTD,
M.MotOcorDescr
,Jan = sum(case when MONTH(O.OcorData) = '1' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Fev = sum(case when MONTH(O.OcorData) = '2' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Mar = sum(case when MONTH(O.OcorData) = '3' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Abr = sum(case when MONTH(O.OcorData) = '4' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Mai = sum(case when MONTH(O.OcorData) = '5' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Jun = sum(case when MONTH(O.OcorData) = '6' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Jul = sum(case when MONTH(O.OcorData) = '7' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Ago = sum(case when MONTH(O.OcorData) = '8' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Setem = sum(case when MONTH(O.OcorData) = '9' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Outub = sum(case when MONTH(O.OcorData) = '10'then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Nov = sum(case when MONTH(O.OcorData) = '11' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Dez = sum(case when MONTH(O.OcorData) = '12' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
From ocorrencia O With(NoLock),
Motivo_Ocor M With(NoLock)
Where
O.MotOcorCodEstr = M.MotOcorCodEstr
AND SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 1, 4) + SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 6, 2) >= SUBSTRING('11/2013', 4, 4) + SUBSTRING('11/2013', 1, 2)
AND SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 1, 4) + SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 6, 2) <= SUBSTRING('02/2014', 4, 4) + SUBSTRING('02/2014', 1, 2)
And M.MotOcorCodEstr not like '01.08%'
GROUP BY o.MotOcorCodEstr, m.MotOcorDescr
Order by Count(o.MotOcorCodEstr) desc
Respostas
-
Boa tarde,
Experimente dessa forma:
,Jan = count(case when MONTH(O.OcorData) = 1 then O.MotOcorCodEstr end) ,Fev = count(case when MONTH(O.OcorData) = 2 then O.MotOcorCodEstr end) , -- ...
Espero que ajude.
Assinatura: http://www.imoveisemexposicao.com.br
- Marcado como Resposta Leandro Massena Moraes quarta-feira, 5 de fevereiro de 2014 18:16
Todas as Respostas
-
Boa tarde,
Experimente dessa forma:
,Jan = count(case when MONTH(O.OcorData) = 1 then O.MotOcorCodEstr end) ,Fev = count(case when MONTH(O.OcorData) = 2 then O.MotOcorCodEstr end) , -- ...
Espero que ajude.
Assinatura: http://www.imoveisemexposicao.com.br
- Marcado como Resposta Leandro Massena Moraes quarta-feira, 5 de fevereiro de 2014 18:16
-
Leandro,
O motivo deve esse é a função agregada em cima de outra função agregada, tente esse script:
with RELAT as
(
Select
O.MotOcorCodEstr,
Count(O.MotOcorCodEstr) AS QTD,
M.MotOcorDescr
,Jan = (case when MONTH(O.OcorData) = '1' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Fev = (case when MONTH(O.OcorData) = '2' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Mar = (case when MONTH(O.OcorData) = '3' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Abr = (case when MONTH(O.OcorData) = '4' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Mai = (case when MONTH(O.OcorData) = '5' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Jun = (case when MONTH(O.OcorData) = '6' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Jul = (case when MONTH(O.OcorData) = '7' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Ago = (case when MONTH(O.OcorData) = '8' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Setem = (case when MONTH(O.OcorData) = '9' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Outub = (case when MONTH(O.OcorData) = '10'then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Nov = (case when MONTH(O.OcorData) = '11' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
,Dez = (case when MONTH(O.OcorData) = '12' then ISnull(Count(O.MotOcorCodEstr),0) else 0 end)
From ocorrencia O With(NoLock),
Motivo_Ocor M With(NoLock)
Where
O.MotOcorCodEstr = M.MotOcorCodEstr
AND SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 1, 4) +
SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 6, 2) >= SUBSTRING('11/2013', 4, 4) + SUBSTRING('11/2013', 1, 2)
AND SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 1, 4) + SUBSTRING(CONVERT(VARCHAR, O.OcorData, 120), 6, 2) <= SUBSTRING('02/2014', 4, 4) + SUBSTRING('02/2014', 1, 2)
And M.MotOcorCodEstr not like '01.08%'
GROUP BY o.MotOcorCodEstr, m.MotOcorDescr
Order by Count(o.MotOcorCodEstr) desc
)
SELECT MotOcorCodEstr, QTD, MotOcorDescr,
sum (Jan),
sum (Fev),
sum (Mar),
sum (Abr),
sum (Mai),
sum (Jun),
sum (Jul),
sum (Ago),
sum (Setem),
sum (Outub),
sum (Nov),
sum (Dez)
From RELAT