QUERY PLAN
Aggregate (cost=261.55..261.56 rows=1 width=8)
-> Sort (cost=261.30..261.34 rows=17 width=934)
Sort Key: p.bargain_date DESC, p.updated_at DESC
-> Subquery Scan on p (cost=260.44..260.95 rows=17 width=934)
-> GroupAggregate (cost=260.44..260.78 rows=17 width=906)
Group Key: p_1.id, pd.user_id, pd.pct
-> Sort (cost=260.44..260.48 rows=17 width=60)
Sort Key: p_1.id, pd.user_id, pd.pct
-> Nested Loop Left Join (cost=2.65..260.09 rows=17 width=60)
-> Hash Left Join (cost=2.37..203.91 rows=17 width=60)
Hash Cond: (p_1.id = pd.project_id)
Filter: ((p_1.created_by = 1001) OR (pd.user_id = 1001))
-> Seq Scan on tbl_projects p_1 (cost=0.00..201.30 rows=60 width=24)
Filter: ((bargain_date >= '2026-01-01'::date) AND (bargain_date <= '2026-12-31'::date) AND (project_status = 3))
-> Hash (cost=2.32..2.32 rows=4 width=40)
-> Subquery Scan on pd (cost=2.23..2.32 rows=4 width=40)
-> HashAggregate (cost=2.23..2.28 rows=4 width=40)
Group Key: t.project_id, r.user_id
-> Hash Right Join (cost=1.07..2.20 rows=4 width=13)
Hash Cond: (r.erp_profit_distribute_id = t.id)
-> Seq Scan on tbl_erp_profit_distribute_pcts r (cost=0.00..1.08 rows=8 width=13)
-> Hash (cost=1.05..1.05 rows=2 width=8)
-> Seq Scan on tbl_erp_profit_distributes t (cost=0.00..1.05 rows=2 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Index Only Scan using tbl_sales_contracts_from_project_idx on tbl_sales_contracts sc (cost=0.28..3.29 rows=1 width=4)
Index Cond: (from_project = p_1.id)