Fernandinha Posted September 2, 2011 Report Share Posted September 2, 2011 (edited) Estou tentando executar a query abaixo fazendo uma contagem nos campos, porém sempre retorna o mesmo resultado par todas as colunas.Poderiam me ajudar, por favor?SELECT MATR.Id , CASE WHEN TP.StatusFrequencia = 'A' THEN COUNT(TP.StatusFrequencia) END AS QtFaltas , CASE WHEN TPA.StatusFrequencia = 'J' THEN COUNT(TPA.StatusFrequencia) END AS QtFaltasJustificadas , CASE WHEN TPB.StatusFrequencia = 'P' THEN COUNT(TPB.StatusFrequencia) END AS QtPresençaFROM MATRICULA_PQ MATRJOIN #TEMP AS TP ON TP.IDMATRICULA = MATR.Id AND TP.STATUSFREQUENCIA = 'A'JOIN #TEMP AS TPA ON TPA.Id = TP.ID AND TPA.IDMATRICULA = TP.IDMATRICULA JOIN #TEMP AS TPB ON TPB.Id = TPA.ID AND TPB.IDMATRICULA = TPA.IDMATRICULA GROUP BY MATR.Id, TP.StatusFrequencia, TPA.StatusFrequencia, TPB.StatusFrequenciaORDER BY MATR.IdObrigada,Fernanda Edited September 2, 2011 by Fernandinha Quote Link to comment Share on other sites More sharing options...
0 Fernandinha Posted September 2, 2011 Author Report Share Posted September 2, 2011 Pessoal,Consegui resolver, segue aí, caso alguém precise...SELECT MATR.Id AS [idMatricula] -- , COUNT (CASE WHEN TP.StatusFrequencia = 'A' THEN 'QtFaltas' END) 'QtFaltas' , COUNT (CASE WHEN TP.StatusFrequencia = 'J' THEN 'QtFaltasJustificadas' END) 'QtFaltasJustificadas' , COUNT (CASE WHEN TP.StatusFrequencia = 'P' THEN 'QtPresença' END) 'QtPresença'FROM MATRICULA_PQ MATRJOIN #TEMP AS TP ON TP.IDMATRICULA = MATR.IdGROUP BY MATR.IdORDER BY MATR.Id Quote Link to comment Share on other sites More sharing options...
Question
Fernandinha
Estou tentando executar a query abaixo fazendo uma contagem nos campos, porém sempre retorna o mesmo resultado par todas as colunas.
Poderiam me ajudar, por favor?
SELECT MATR.Id
, CASE WHEN TP.StatusFrequencia = 'A' THEN COUNT(TP.StatusFrequencia) END AS QtFaltas
, CASE WHEN TPA.StatusFrequencia = 'J' THEN COUNT(TPA.StatusFrequencia) END AS QtFaltasJustificadas
, CASE WHEN TPB.StatusFrequencia = 'P' THEN COUNT(TPB.StatusFrequencia) END AS QtPresença
FROM MATRICULA_PQ MATR
JOIN #TEMP AS TP ON TP.IDMATRICULA = MATR.Id AND TP.STATUSFREQUENCIA = 'A'
JOIN #TEMP AS TPA ON TPA.Id = TP.ID AND TPA.IDMATRICULA = TP.IDMATRICULA
JOIN #TEMP AS TPB ON TPB.Id = TPA.ID AND TPB.IDMATRICULA = TPA.IDMATRICULA
GROUP BY MATR.Id, TP.StatusFrequencia, TPA.StatusFrequencia, TPB.StatusFrequencia
ORDER BY MATR.Id
Obrigada,
Fernanda
Edited by FernandinhaLink to comment
Share on other sites
1 answer to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.