Add result from POINTS column

Asked

Viewed 61 times

0

Good afternoon!

I have the following situation: I have a table with the results of some football games. I managed to assemble a SELECT and assign 3 points to the winner and 1 points to the draws. Now I need to add all the points of each team and assemble a table from the first to the last placed. What I’m unable to do and that sum of points. Follow the SELECT I’ve done so far:

inserir a descrição da imagem aqui

SELECT CDJOGO, CDEQUIPE, NMABREVIADO, CDEQUIPEVENCEDORA,

CASE 
    WHEN CDEQUIPE = CDEQUIPEVENCEDORA THEN +3
    WHEN CDEQUIPEVENCEDORA IS NULL THEN  +1
END AS PONTOS

FROM EQUIPE AS E
JOIN JOGO AS J ON E.CDEQUIPE = J.CDEQUIPECASA OR E.CDEQUIPE = J.CDEQUIPEVISITANTE
GROUP BY CDJOGO, CDEQUIPE, NMABREVIADO, CDEQUIPEVENCEDORA
  • 1

    Edit your question and put the table structure.

1 answer

0


Would this way have the result that you ask?

SELECT CDEQUIPE, NMABREVIADO, PONTOS = sum(PONTOS)
FROM
(
SELECT CDJOGO, CDEQUIPE, NMABREVIADO, CDEQUIPEVENCEDORA,

CASE 
    WHEN CDEQUIPE = CDEQUIPEVENCEDORA THEN +3
    WHEN CDEQUIPEVENCEDORA IS NULL THEN  +1
END AS PONTOS

FROM EQUIPE AS E
JOIN JOGO AS J ON E.CDEQUIPE = J.CDEQUIPECASA OR E.CDEQUIPE = J.CDEQUIPEVISITANTE
GROUP BY CDJOGO, CDEQUIPE, NMABREVIADO, CDEQUIPEVENCEDORA
)
GROUP BY CDEQUIPE, NMABREVIADO
ORDER BY PONTOS DESC

Browser other questions tagged

You are not signed in. Login or sign up in order to post.