(select sum(val) as val,company_id,user_id from logistics.sales_channelrebateredeemed where ancestor_id={aid} and {r[1]}=company_id group by company_id,user_id)a
left outer join
(select id,name from logistics.company_company )b on a.company_id=b.id
left outer join
(select id,username from logistics.company_user )c on a.user_id=c.id
"""
s=f"""SELECT SUM(t.val), t.company_name, ARRAY_AGG(t.user_name) FROM
(select sum(val) as val,company_id,user_id from logistics.sales_channelrebateredeemed where ancestor_id={aid} and {r[1]}=company_id group by company_id,user_id)a
left outer join
(select id,name from logistics.company_company) b on a.company_id=b.id
left outer join
(select id,username from logistics.company_user) c on a.user_id=c.id