QUERY PLAN
Aggregate (cost=871.44..871.45 rows=1 width=32)
-> GroupAggregate (cost=870.18..870.92 rows=42 width=36)
Group Key: sub.id2
-> Sort (cost=870.18..870.29 rows=42 width=36)
Sort Key: sub.id2
-> Subquery Scan on sub (cost=868.52..869.05 rows=42 width=36)
-> Sort (cost=868.52..868.63 rows=42 width=571)
Sort Key: (CASE WHEN (t0_1.id IS NOT NULL) THEN t0_1.remind_date ELSE sc.signed_at END) DESC
-> Nested Loop (cost=819.95..867.39 rows=42 width=571)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.77 rows=1 width=4)
Filter: ((sales_id = 1001) AND (group_id = 1))
-> Hash Right Join (cost=819.95..865.21 rows=42 width=52)
Hash Cond: (t0.id = sc.id)
-> HashAggregate (cost=201.10..222.45 rows=1708 width=36)
Group Key: t0.id
-> Hash Right Join (cost=139.60..190.54 rows=2112 width=17)
Hash Cond: (t1.contract_id = t0.id)
-> Seq Scan on tbl_sales_contract_invoices t1 (cost=0.00..45.33 rows=2133 width=10)
-> Hash (cost=118.25..118.25 rows=1708 width=11)
-> Seq Scan on tbl_sales_contracts t0 (cost=0.00..118.25 rows=1708 width=11)
Filter: need_analysis
-> Hash (cost=618.33..618.33 rows=42 width=20)
-> Hash Right Join (cost=603.22..618.33 rows=42 width=20)
Hash Cond: (t0_1.contract_id = sc.id)
Filter: (((t0_1.id IS NOT NULL) AND (t0_1.paid_amount <> '0'::numeric)) OR ((t0_1.id IS NULL) AND (sc.erp_settle_amount > '0'::numeric)))
-> HashAggregate (cost=380.52..386.78 rows=626 width=67)
Group Key: t0_1.id
-> Hash Join (cost=180.87..378.96 rows=626 width=18)
Hash Cond: (p.id = t1_1.from_project)
-> Seq Scan on tbl_projects p (cost=0.00..185.55 rows=1703 width=4)
Filter: (project_status = 3)
-> Hash (cost=166.34..166.34 rows=1163 width=22)
-> Hash Join (cost=139.24..166.34 rows=1163 width=22)
Hash Cond: (t0_1.contract_id = t1_1.id)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0_1 (cost=0.00..23.95 rows=1195 width=18)
-> Hash (cost=118.25..118.25 rows=1679 width=8)
-> Seq Scan on tbl_sales_contracts t1_1 (cost=0.00..118.25 rows=1679 width=8)
Filter: (need_analysis AND (from_project IS NOT NULL))
-> Hash (cost=222.16..222.16 rows=43 width=18)
-> Hash Join (cost=99.40..222.16 rows=43 width=18)
Hash Cond: (sc.client_id = c.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..118.25 rows=1713 width=22)
Filter: build_by_project
-> Hash (cost=98.78..98.78 rows=50 width=8)
-> Bitmap Heap Scan on tbl_clients c (cost=4.79..98.78 rows=50 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.78 rows=67 width=0)
Index Cond: (sales_rep = 1001)