So, i have this query
JavaScript
x
SELECT nome_equipa,
count(id_golo) AS golos_marcados FROM equipa,golo
WHERE golo.equipa_id_equipa = id_equipa
GROUP BY golo.equipa_id_equipa, nome_equipa ORDER BY count(id_golo)DESC
which outputs this table
then i have this other query
JavaScript
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
which outputs this:
All i want to do is place the two columns side by side without bugs.
tried this
JavaScript
select nome_equipa, count(id_golo) as golos_marcados,
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, golo
where golo.equipa_id_equipa = id_equipa
group by nome_equipa
order by vitorias desc
but it returned this:
Here is the ER diagram:
Advertisement
Answer
Ok, Is hard to follow up the logic with the above information, so I can’t give you the exact query you have to run. But What I can suggest how to arrive to it.
I would recommend to use WITH statement. (https://www.postgresql.org/docs/9.1/queries-with.html)
Sth like:
JavaScript
WITH goals AS (
SELECT
nome_equipa,
count(id_golo) as golos_marcados
FROM equipa, golo
WHERE golo.equipa_id_equipa = id_equipa
GROUP BY nome_equipa
)
SELECT nome_equipa,
g.golos_marcados,
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 TABLE IS NO NEEDED
LEFT JOIN goals g
ON goals.nome_equipa = jogo.nome_equipa
GROUP BY nome_equipa
Then you join with the other query and by nome_equipa and add the column. And that’s it.