The problem is simple but I can not understand where I am wrong, I need to check the amount of results of the following sql:
SELECT
f.id,sum(p.valor) as valor_fatura, f.valor_desconto as desconto,
(f.numero || '/' || f.ano_fatura ) as numero,
(sum(p.valor) + f.valor_desconto + f.valor_acrescimo)as
valor,f.valor_acrescimo as acrescimo, f.emissao_em,
f.vence_em, f.situacao, f.juros_acrescimo, hb.dt_hr_inclusao::date AS
vencimento_ultimo_recalculo,
f.multa_acrescimo, f.is_gera_boleto, f.qtd_taxa_recalculo,
f.valor_taxa_recalculo, f.is_confianca,
(sumid(p.is_baixado) = sumid(true)) as is_baixado
FROM parcels p
JOIN sales s ON (p.sale_id = s.id)
JOIN faturas f ON (p.fatura_cobranca_id = f.id)
LEFT JOIN (SELECT (array_agg(dt_hr_inclusao order by dt_hr_inclusao
desc))[1] as dt_hr_inclusao,
(array_agg(cod_mov_remessa_id order by dt_hr_inclusao desc))[1] as
cod_mov_remessa_id,
hb.fatura_id FROM historico_remessa_boleto hb GROUP BY hb.fatura_id
) hb ON (f.id = hb.fatura_id and hb.cod_mov_remessa_id = 6)
WHERE p.is_baixado = false AND f.vence_em is not null AND is_gera_boleto
AND consignatario_id = 6512
GROUP BY f.id, f.numero, f.ano_fatura, f.emissao_em, f.vence_em,
f.situacao, f.juros_acrescimo, f.multa_acrescimo, f.is_gera_boleto,
f.valor_desconto, f.valor_acrescimo, hb.dt_hr_inclusao::date
The problem is that when I run this sql it brings me 6 results, to know the amount now I would do this:
SELECT
count(p.id)
FROM parcels p
JOIN sales s ON (p.sale_id = s.id)
JOIN faturas f ON (p.fatura_cobranca_id = f.id)
LEFT JOIN (SELECT (array_agg(dt_hr_inclusao order by dt_hr_inclusao
desc))[1] as dt_hr_inclusao,
(array_agg(cod_mov_remessa_id order by dt_hr_inclusao desc))[1] as
cod_mov_remessa_id,
hb.fatura_id FROM historico_remessa_boleto hb GROUP BY hb.fatura_id
) hb ON (f.id = hb.fatura_id and hb.cod_mov_remessa_id = 6)
WHERE p.is_baixado = false AND f.vence_em is not null AND is_gera_boleto
AND consignatario_id = 6512
Now she returns me 11 results in the count. I'm not good at sql my focus is front end so I'm kind of lost where I can go wrong in case anyone needs some info I did not put just ask in the comments.
When I leave the query with group by it returns me 6 rows of results where I can only have a result with the total amount.