select
t.id as transaction_id,
o.id as operation_id,
t.effective_date,
cat.mnemonics as credit_account_type,
t.value as transaction_value,
count(*) as rows_after_join,
sum(t.value) as value_after_join,
string_agg(
distinct cast(io.number as text),
', '
) as payment_order_numbers
from back_office.transaction t
join back_office.operation o
on o.id = t.operation_id
join back_office.account ca
on ca.id = t.credit_account_id
join back_office.account_type cat
on cat.id = ca.account_type_id
left join back_office.incoming_order_operation ioo
on ioo.operation_id = o.id
left join back_office.incoming_order io
on io.id = ioo.incoming_order_id
where ca.owner_sharer_id = 14329238
and o.approved is not null
and o.deleted is null
and t.effective_date >= cast('2016-01-21' as date)
and t.effective_date < cast('2016-01-22' as date)
group by
t.id,
o.id,
t.effective_date,
cat.mnemonics,
t.value
order by t.id;