Jump to content
Fórum Script Brasil
  • 0

(Resolvido)Query


Fernandinha

Question

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 Fernandinha
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

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 MATR

JOIN #TEMP AS TP ON TP.IDMATRICULA = MATR.Id

GROUP BY MATR.Id

ORDER BY MATR.Id

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...