QUERY PLAN
Aggregate (cost=661.93..661.94 rows=1 width=8)
-> Sort (cost=660.79..660.98 rows=76 width=233)
Sort Key: sc.signed_at DESC, sc.created_at DESC
-> Nested Loop (cost=507.75..658.41 rows=76 width=233)
-> Hash Left Join (cost=507.47..521.20 rows=76 width=4)
Hash Cond: (sc_1.id = spc.id)
Filter: (((scpcrd.remind_date >= '2025-08-01'::date) AND (scpcrd.remind_date <= '2025-08-31'::date)) OR ((CASE WHEN (spc.erp_settle_amount IS NULL) THEN '0'::numeric ELSE spc.erp_settle_amount END < (SubPlan 1)) AND (CASE WHEN (scpcrd.remind_date <= '2025-08-09'::date) THEN ((SubPlan 2) - CASE WHEN (spc.erp_settle_amount IS NULL) THEN '0'::numeric ELSE spc.erp_settle_amount END) ELSE '0'::numeric END > '0'::numeric)))
-> Hash Left Join (cost=231.16..243.38 rows=572 width=12)
Hash Cond: (scpcrd.contract_id = sc_1.id)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd (cost=0.00..10.72 rows=572 width=8)
-> Hash (cost=206.07..206.07 rows=2007 width=8)
-> Seq Scan on tbl_purchase_contracts sc_1 (cost=0.00..206.07 rows=2007 width=8)
-> Hash (cost=251.23..251.23 rows=2007 width=10)
-> HashAggregate (cost=211.09..231.16 rows=2007 width=10)
Group Key: spc.id
-> Seq Scan on tbl_purchase_contracts spc (cost=0.00..206.07 rows=2007 width=10)
SubPlan 1
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
SubPlan 2
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_1 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
-> Index Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc (cost=0.28..1.81 rows=1 width=16)
Index Cond: (id = scpcrd.contract_id)
Filter: ((NOT is_draft) AND (NOT is_refuse))