QUERY PLAN
Aggregate (cost=227.55..227.56 rows=1 width=104)
-> Sort (cost=227.32..227.35 rows=10 width=571)
Sort Key: (CASE WHEN (scpcrds.id IS NOT NULL) THEN scpcrds.remind_date ELSE sc.signed_at END) DESC
-> Nested Loop Left Join (cost=196.07..227.16 rows=10 width=571)
-> Nested Loop Left Join (cost=195.92..209.05 rows=1 width=132)
Join Filter: (t1_1.id = sc.id)
-> Nested Loop Left Join (cost=158.58..171.67 rows=1 width=104)
Join Filter: (t0_1.id = sc.id)
-> Hash Left Join (cost=51.99..62.81 rows=1 width=96)
Hash Cond: (sc.id = scpcrds.contract_id)
Filter: (((scpcrds.id IS NOT NULL) AND (scpcrds.paid_amount <> '0'::numeric)) OR ((scpcrds.id IS NULL) AND (sc.erp_settle_amount > '0'::numeric)))
-> Hash Left Join (cost=10.45..21.24 rows=2 width=48)
Hash Cond: (sc.client_id = c.id)
Filter: ((c.sales_rep = 5493) OR (c.sales_rep IS NULL))
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..10.70 rows=35 width=48)
Filter: build_by_project
-> Hash (cost=10.20..10.20 rows=20 width=8)
-> Seq Scan on tbl_clients c (cost=0.00..10.20 rows=20 width=8)
-> Hash (cost=41.50..41.50 rows=3 width=52)
-> Subquery Scan on scpcrds (cost=41.46..41.50 rows=3 width=52)
-> Group (cost=41.46..41.47 rows=3 width=93)
Group Key: t0.id
-> Sort (cost=41.46..41.47 rows=3 width=52)
Sort Key: t0.id
-> Hash Join (cost=22.19..41.43 rows=3 width=52)
Hash Cond: (t0.contract_id = t1.id)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0 (cost=0.00..16.70 rows=670 width=52)
-> Hash (cost=22.18..22.18 rows=1 width=4)
-> Hash Join (cost=11.39..22.18 rows=1 width=4)
Hash Cond: (t1.from_project = p.id)
-> Seq Scan on tbl_sales_contracts t1 (cost=0.00..10.70 rows=35 width=8)
Filter: (need_analysis AND (from_project IS NOT NULL))
-> Hash (cost=11.38..11.38 rows=1 width=4)
-> Seq Scan on tbl_projects p (cost=0.00..11.38 rows=1 width=4)
Filter: (project_status = 3)
-> HashAggregate (cost=106.59..107.29 rows=70 width=44)
Group Key: t0_1.id
-> Hash Right Join (cost=23.85..106.24 rows=70 width=36)
Hash Cond: ("*SELECT* 1".contract_id = t0_1.id)
-> Append (cost=12.28..94.46 rows=16 width=124)
-> Subquery Scan on "*SELECT* 1" (cost=12.28..31.30 rows=8 width=124)
-> Hash Join (cost=12.28..31.20 rows=8 width=120)
Hash Cond: (r.erp_collect_ticket_id = t.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r (cost=0.00..17.00 rows=696 width=44)
Filter: (sales_contract_id IS NOT NULL)
-> Hash (cost=12.25..12.25 rows=2 width=52)
-> Seq Scan on tbl_erp_collect_tickets t (cost=0.00..12.25 rows=2 width=52)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash Join (cost=12.39..31.92 rows=4 width=124)
Hash Cond: (r_1.erp_cash_cost_ticket_id = t_1.id)
-> Seq Scan on tbl_erp_cash_cost_ticket_sales_contract_settles r_1 (cost=0.00..17.50 rows=746 width=44)
Filter: (sales_contract_id IS NOT NULL)
-> Hash (cost=12.38..12.38 rows=1 width=48)
-> Seq Scan on tbl_erp_cash_cost_tickets t_1 (cost=0.00..12.38 rows=1 width=48)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash Join (cost=12.26..31.16 rows=4 width=124)
Hash Cond: (r_2.erp_payment_ticket_id = t_2.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r_2 (cost=0.00..17.00 rows=696 width=44)
Filter: (sales_contract_id IS NOT NULL)
-> Hash (cost=12.25..12.25 rows=1 width=48)
-> Seq Scan on tbl_erp_payment_tickets t_2 (cost=0.00..12.25 rows=1 width=48)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash (cost=10.70..10.70 rows=70 width=4)
-> Seq Scan on tbl_sales_contracts t0_1 (cost=0.00..10.70 rows=70 width=4)
-> GroupAggregate (cost=37.34..37.36 rows=1 width=36)
Group Key: t1_1.id
-> Sort (cost=37.34..37.35 rows=1 width=36)
Sort Key: t1_1.id
-> Nested Loop (cost=11.28..37.33 rows=1 width=36)
-> Hash Join (cost=11.14..32.87 rows=2 width=40)
Hash Cond: (t0_2.contract_id = t1_1.id)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0_2 (cost=0.00..21.73 rows=4 width=36)
Filter: (((status = 1) AND (overdue_days IS NOT NULL)) OR ((status = 0) AND (remind_date <= '2026-06-02'::date)))
-> Hash (cost=10.70..10.70 rows=35 width=8)
-> Seq Scan on tbl_sales_contracts t1_1 (cost=0.00..10.70 rows=35 width=8)
Filter: (need_analysis AND (from_project IS NOT NULL))
-> Index Scan using tbl_projects_pkey on tbl_projects p_1 (cost=0.14..1.53 rows=1 width=4)
Index Cond: (id = t1_1.from_project)
Filter: (project_status = 3)
-> Index Only Scan using tbl_sales_group_relationships_pkey on tbl_sales_group_relationships sgr (cost=0.15..17.95 rows=10 width=4)
Index Cond: (sales_id = c.sales_rep)