I have two tables, Cliente
and Parcela
.
The Parcela
table has id do cliente
as foreign key, a pagamento
column that is null
or contains the payment date in date
format and a valor
column that is float
and contains the value paid or null
case opened.
I need to add all paid installments and all open installments of all clients.
I'm trying to do this, but it's adding up all the plots and showing only the first client.
select c.nome_cliente,
SUM(CASE WHEN p.pagamento is not null THEN p.valor ELSE 0 END) as 'PAGO',
SUM(CASE WHEN p.pagamento is null THEN p.valor ELSE 0 END) as 'ABERTO'
from cliente c
LEFT JOIN parcela p
ON c.id = p.id_cliente