QUERY PLAN
Aggregate (cost=559.18..559.19 rows=1 width=8)
-> Sort (cost=533.63..537.89 rows=1703 width=870)
Sort Key: p.erp_sales_amount, p.updated_at DESC
-> Subquery Scan on p (cost=395.40..442.24 rows=1703 width=870)
-> HashAggregate (cost=395.40..425.21 rows=1703 width=842)
Group Key: p_1.id, pd.user_id, pd.pct
-> Hash Left Join (cost=244.71..378.37 rows=1703 width=88)
Hash Cond: (p_1.id = o2.project_id)
-> Hash Left Join (cost=220.95..350.14 rows=1703 width=56)
Hash Cond: (p_1.id = pd.project_id)
-> Hash Right Join (cost=206.84..329.63 rows=1703 width=20)
Hash Cond: (sc.from_project = p_1.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..118.25 rows=1725 width=4)
-> Hash (cost=185.55..185.55 rows=1703 width=20)
-> Seq Scan on tbl_projects p_1 (cost=0.00..185.55 rows=1703 width=20)
Filter: (project_status = 3)
-> Hash (cost=14.10..14.10 rows=1 width=40)
-> Subquery Scan on pd (cost=14.06..14.10 rows=1 width=40)
-> GroupAggregate (cost=14.06..14.09 rows=1 width=40)
Group Key: t.project_id, r.user_id
-> Sort (cost=14.06..14.07 rows=1 width=13)
Sort Key: t.project_id, r.user_id
-> Nested Loop Left Join (cost=0.00..14.05 rows=1 width=13)
Join Filter: (t.id = r.erp_profit_distribute_id)
-> Seq Scan on tbl_erp_profit_distributes t (cost=0.00..12.88 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts r (cost=0.00..1.08 rows=8 width=13)
-> Hash (cost=23.74..23.74 rows=1 width=36)
-> Subquery Scan on o2 (cost=23.69..23.74 rows=1 width=36)
-> GroupAggregate (cost=23.69..23.73 rows=1 width=68)
Group Key: t_1.project_id
-> Sort (cost=23.69..23.70 rows=4 width=8)
Sort Key: t_1.project_id
-> Hash Right Join (cost=9.67..23.65 rows=4 width=8)
Hash Cond: (eipss.erp_sales_outbound_id = t_1.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.51 rows=551 width=8)
-> Hash (cost=9.66..9.66 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_1 (cost=0.00..9.66 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (verify_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (verify_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))