QUERY PLAN
Aggregate (cost=6582.46..6582.47 rows=1 width=32)
-> GroupAggregate (cost=976.31..6580.17 rows=76 width=132)
Group Key: scpcrd.contract_id
Filter: (CASE WHEN (max(CASE WHEN (spc.erp_settle_amount IS NULL) THEN '0'::numeric ELSE spc.erp_settle_amount END) > '0'::numeric) THEN max(CASE WHEN (spc.erp_settle_amount IS NULL) THEN '0'::numeric ELSE spc.erp_settle_amount END) ELSE '0'::numeric END < max((SubPlan 5)))
-> Sort (cost=976.31..976.50 rows=76 width=14)
Sort Key: scpcrd.contract_id
-> Hash Right Join (cost=925.51..973.94 rows=76 width=14)
Hash Cond: (spc.id = sc.id)
-> 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)
-> Hash (cost=713.48..713.48 rows=76 width=12)
-> Nested Loop Left Join (cost=663.16..713.48 rows=76 width=12)
-> Hash Join (cost=662.88..675.11 rows=76 width=8)
Hash Cond: (scpcrd.id = scpcrd_1.id)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd (cost=0.00..10.72 rows=572 width=12)
-> 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_1.id)
Filter: (((scpcrd_1.remind_date >= '2025-08-01'::date) AND (scpcrd_1.remind_date <= '2025-08-31'::date)) OR ((CASE WHEN (spc_1.erp_settle_amount IS NULL) THEN '0'::numeric ELSE spc_1.erp_settle_amount END < (SubPlan 6)) AND (CASE WHEN (scpcrd_1.remind_date <= '2025-08-09'::date) THEN ((SubPlan 7) - CASE WHEN (spc_1.erp_settle_amount IS NULL) THEN '0'::numeric ELSE spc_1.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_1.id
-> Seq Scan on tbl_purchase_contracts spc_1 (cost=0.00..206.07 rows=2007 width=10)
SubPlan 6
-> 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_1.remind_date) AND (contract_id = scpcrd_1.contract_id))
SubPlan 7
-> 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_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.50 rows=1 width=4)
Index Cond: (id = scpcrd.contract_id)
SubPlan 4
-> Aggregate (cost=15.02..15.03 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub3_1 (cost=0.00..15.01 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (remind_date < '2025-08-01'::date) AND (contract_id = scpcrd.contract_id))
SubPlan 3
-> Aggregate (cost=15.02..15.03 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub3 (cost=0.00..15.01 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (remind_date < '2025-08-01'::date) AND (contract_id = scpcrd.contract_id))
SubPlan 2
-> Aggregate (cost=16.45..16.46 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub2 (cost=0.00..16.44 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (remind_date >= '2025-08-01'::date) AND (remind_date <= '2025-08-31'::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))
SubPlan 5
-> 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))