2020-12-28

How to sum columns in sql

I know it may be basic, but i'm noob to SQL. So, i have this query:

select nome_equipa, 
sum(case when (resultado_visitado = resultado_visitante) and jogo.equipa_id_equipa = id_equipa or (resultado_visitante = resultado_visitado) and jogo.equipa_id_equipa1 = id_equipa  then 1 else 0 end ) as empates,
sum(case when (resultado_visitado > resultado_visitante) and jogo.equipa_id_equipa = id_equipa or (resultado_visitante > resultado_visitado) and jogo.equipa_id_equipa1 = id_equipa  then 1 else 0 end ) as vitorias,
sum(case when (resultado_visitado < resultado_visitante) and jogo.equipa_id_equipa = id_equipa or (resultado_visitante < resultado_visitado) and jogo.equipa_id_equipa1 = id_equipa  then 1 else 0 end ) as derrotas
from jogo, equipa
group by nome_equipa
order by vitorias desc;

Outputting this:

enter image description here

so, the sum columns are not in my db and are calculated by SQL. These columns are the number of victories, losses and draws of a team. now i want to display the number of points of each team. I thought that would be easy, like adding just (empates*1 + derrotas*0 + vitorias*3) as pontos to the select, but it does not work. i think it's because the columns i am trying to sum are calculated and not in the db.

Can someone help me?



from Recent Questions - Stack Overflow https://ift.tt/2WRCRUe
https://ift.tt/2WVgNYN

No comments:

Post a Comment