select distinct extract(month from principal.vencimento) mes, extract(year from principal.vencimento) ano, (select coalesce(sum(detalhe.valor_pagamento),0) from contas_pagar_liq detalhe where detalhe.vencimento < current_date and extract(month from detalhe.vencimento) = extract(month from principal.vencimento) and extract(year from detalhe.vencimento) = extract(year from principal.vencimento) ) + (select coalesce(sum(detalhe.valor - detalhe.valor_pagamento),0) from contas_pagar detalhe where detalhe.vencimento < current_date and extract(month from detalhe.vencimento) = extract(month from principal.vencimento) and extract(year from detalhe.vencimento) = extract(year from principal.vencimento)) carteira, (select coalesce(sum(detalhe.valor_pagamento),0) from contas_pagar_liq detalhe where detalhe.dataliq < current_date and extract(month from detalhe.dataliq) = extract(month from principal.vencimento) and extract(year from detalhe.dataliq) = extract(year from principal.vencimento) ) realizado, coalesce(sum(Principal.valor - Principal.valor_pagamento),0) aPagar from contas_pagar principal where principal.vencimento < current_date order by extract(year from principal.vencimento),extract(month from principal.vencimento)