QUERY PLAN
Aggregate (cost=1031.31..1031.32 rows=1 width=32)
-> GroupAggregate (cost=990.40..1031.26 rows=3 width=132)
Group Key: scpcrd.contract_id
-> Sort (cost=990.40..990.41 rows=3 width=14)
Sort Key: scpcrd.contract_id
-> Hash Left Join (cost=939.47..990.37 rows=3 width=14)
Hash Cond: (sc.id = p.id)
Filter: ((CASE WHEN (p.collected IS NULL) THEN '0'::numeric ELSE p.collected END < (SubPlan 2)) AND (((scpcrd.remind_date >= '2025-08-01'::date) AND (scpcrd.remind_date <= '2025-08-31'::date)) OR ((CASE WHEN (p.collected IS NULL) THEN '0'::numeric ELSE p.collected END < (SubPlan 3)) AND (CASE WHEN (scpcrd.remind_date <= '2025-08-09'::date) THEN ((SubPlan 4) - CASE WHEN (p.collected IS NULL) THEN '0'::numeric ELSE p.collected END) ELSE '0'::numeric END > '0'::numeric))))
-> Nested Loop Left Join (cost=663.16..713.87 rows=73 width=12)
-> Hash Join (cost=662.88..676.49 rows=73 width=8)
Hash Cond: (scpcrd.id = scpcrd_1.id)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd (cost=0.00..12.15 rows=553 width=12)
Filter: (remind_date <= '2025-08-09'::date)
-> Hash (cost=661.93..661.93 rows=76 width=4)
-> Group (cost=660.79..661.17 rows=76 width=4)
Group Key: scpcrd_1.id
-> Sort (cost=660.79..660.98 rows=76 width=4)
Sort Key: scpcrd_1.id
-> Nested Loop (cost=507.75..658.41 rows=76 width=4)
-> Hash Left Join (cost=507.47..521.20 rows=76 width=8)
Hash Cond: (sc_2.id = spc.id)
Filter: (((scpcrd_1.remind_date >= '2025-08-01'::date) AND (scpcrd_1.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 5)) AND (CASE WHEN (scpcrd_1.remind_date <= '2025-08-09'::date) THEN ((SubPlan 6) - 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=16)
Hash Cond: (scpcrd_1.contract_id = sc_2.id)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd_1 (cost=0.00..10.72 rows=572 width=12)
-> Hash (cost=206.07..206.07 rows=2007 width=8)
-> Seq Scan on tbl_purchase_contracts sc_2 (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 5
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_4 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd_1.remind_date) AND (contract_id = scpcrd_1.contract_id))
SubPlan 6
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_5 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd_1.remind_date) AND (contract_id = scpcrd_1.contract_id))
-> Index Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc_1 (cost=0.28..1.81 rows=1 width=4)
Index Cond: (id = scpcrd_1.contract_id)
Filter: ((NOT is_draft) AND (NOT is_refuse))
-> Index Only Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc (cost=0.28..0.51 rows=1 width=4)
Index Cond: (id = scpcrd.contract_id)
-> Hash (cost=251.23..251.23 rows=2007 width=10)
-> Subquery Scan on p (cost=211.09..251.23 rows=2007 width=10)
-> HashAggregate (cost=211.09..231.16 rows=2007 width=10)
Group Key: spc_1.id
-> Seq Scan on tbl_purchase_contracts spc_1 (cost=0.00..206.07 rows=2007 width=10)
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))
SubPlan 3
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_2 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
SubPlan 4
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_3 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
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))