QUERY PLAN
Aggregate (cost=279.71..279.72 rows=1 width=32)
-> Limit (cost=279.56..279.59 rows=10 width=32)
-> Sort (cost=279.56..284.56 rows=1998 width=32)
Sort Key: inner_sub.erp_revenue DESC
-> Subquery Scan on inner_sub (cost=171.45..236.38 rows=1998 width=32)
-> HashAggregate (cost=171.45..216.40 rows=1998 width=68)
Group Key: c.id
Filter: ((COALESCE(sum(o.outbound_amount), '0'::numeric) + COALESCE(sum(eoi.amount), '0'::numeric)) > '0'::numeric)
-> Hash Left Join (cost=26.98..146.47 rows=1998 width=68)
Hash Cond: (c.id = eoi.client_id)
-> Hash Left Join (cost=25.23..139.46 rows=1998 width=36)
Hash Cond: (c.id = o.client_id)
-> Seq Scan on tbl_clients c (cost=0.00..108.98 rows=1998 width=4)
Filter: (NOT deleted)
-> Hash (cost=24.72..24.72 rows=41 width=36)
-> Subquery Scan on o (cost=23.79..24.72 rows=41 width=36)
-> HashAggregate (cost=23.79..24.31 rows=41 width=68)
Group Key: t.client_id
-> Hash Right Join (cost=8.95..23.10 rows=138 width=9)
Hash Cond: (eipss.erp_sales_outbound_id = t.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.65 rows=565 width=9)
-> Hash (cost=8.36..8.36 rows=47 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t (cost=0.00..8.36 rows=47 width=8)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.74..1.74 rows=1 width=36)
-> Subquery Scan on eoi (cost=1.71..1.74 rows=1 width=36)
-> GroupAggregate (cost=1.71..1.73 rows=1 width=36)
Group Key: t_1.client_id
-> Sort (cost=1.71..1.72 rows=1 width=10)
Sort Key: t_1.client_id
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..1.70 rows=1 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))