--select * from cll_agreement where code = '002463' select collection_dividend_id, min(collection_id) collection_id, min(cvl.tax_id) tax_id, cll_c.code, cll_c.description, max(tax_value) tax_value, max(cdl.vat) tax_vat, coalesce(max(value_increase),0.00) value_increase, coalesce(sum(agreement_increase),0.00) agreement_increase, coalesce(max(value_surcharge),0.00) value_surcharge, coalesce(sum(agreement_surcharge),0.00) agreement_surcharge, coalesce(sum(case when vouchers_id is null then penalty_fee else 0.00 end ),0.00) penalty_fee, coalesce(sum(case when not vouchers_id is null then penalty_fee else 0.00 end ),0.00) agreement_penalty_fee, coalesce(max(value_discount),0.00) value_discount, coalesce(sum(agreement_discount),0.00) agreement_discount, min(cdl.date_due) as date_due from cll_vouchers_line cvl inner join cll_dividend_line cdl on cdl.id = cvl.collection_dividend_id inner join cll_collection cll_c on cll_c.id = cvl.collection_id where cvl.agreement_id = 2761 group by cvl.collection_dividend_id, cll_c.id order by cvl.collection_dividend_id SELECT x.* FROM public.cll_vouchers_line x,cll_dividend_line y,cll_collection z WHERE x.collection_dividend_id = y.id and y.cll_collection_id =z.id and z.create_uid =43 and x.tax_id =3 and x.vouchers_id = 9859195 ORDER BY z.id,x.agreement_id