0
Good morning, I’m trying to solve this but I can’t, in this select he has a field called emprestimo_id
. I need to sum the remaining amount of each loan into one subquery calling for "remainder", but what happens is that it sums up the value of all loans. I need it to show only the sum of each loan number.
SELECT
id,
valor,
data_pagamento,
(SELECT nome_fantasia FROM clientes WHERE id = cliente_id) as cliente,
(SELECT observacoes FROM clientes WHERE id = cliente_id) as observacoes,
(SELECT CONCAT(endereco, ' N: ',numero ) FROM clientes WHERE id = cliente_id) as endereco,
data_recebimento,
prazo,
cliente_id,
emprestimo_id,
valor_pago,
forma_pagamento,
(SELECT (SELECT parcelas * valor_parcela FROM emprestimo WHERE id = emprestimo_id) - SUM(valor_pago) as saldo_devedor FROM parcelas WHERE emprestimo_id = emprestimo_id AND juros != 'sim' ) as restante
FROM
parcelas
WHERE data_pagamento BETWEEN 20210505 AND 20210505 AND emprestimo_id NOT IN (SELECT renegociacao FROM emprestimo WHERE cliente_id = cliente_id) AND emprestimo_id IN (SELECT id FROM emprestimo WHERE quitado != 'sim' AND cliente_id IN (SELECT id FROM clientes WHERE forma_pagamento = 'DINHEIRO' AND usuario_cobranca = 'FULANO'))
look just look at the query, which by the way is already very complicated, has 3 select in nested that can give performance problems, but this is another point, so it is difficult to help, it would be easier if you put example of the data, how it is returning and how would be expected
– Ricardo Pontual