4
I have 3 tables faturas
, parcelas
and pagamentos
. Now I need to mount an SQL to count the installments that are not paid. I have tried a SUM(CASE
but it didn’t work out.
Table structure faturas
:
id_fa
tipo
data_emissao
valor_fatura
Table structure parcelas
:
id_pa
id_fatura
data_vcto
valor_parcela
Table structure pagamentos
:
id_pg
id_parcela
data_pgto
valor_bruto
valor_desconto
valor_juros
valor_multa
valor_pago
My attempt to consult:
SELECT
id_pa,
SUM(CASE WHEN lc_pagamentos.id_parcela=lc_parcelas.id_pa THEN lc_pagamentos.valor_bruto ELSE 0 END) AS vBruto
FROM lc_faturas, lc_parcelas, lc_pagamentos
WHERE lc_faturas.ID=lc_parcelas.ID
This way it only counts the number of installments, but I need you to compare the field valor_parcela
table parcelas
with the sum of the field valor_bruto
table pagamentos
and return only the records that are different, ie return me the portion that is not settled.
Post the structure of the tables involved in the process.
– Marco Aurélio Deleu
vc want to show only HOW many plots have not been paid, or want to LIST the plots not paid???
– Thomas Lima
I need both situations, but then I can take advantage of the same sql and count with mysql_num_rows. What I’m really suffering from is the sql mount
– Marcos A. Silva