QUERY PLAN
Aggregate (cost=899.14..899.15 rows=1 width=32)
-> Hash Left Join (cost=762.66..898.73 rows=83 width=42)
Hash Cond: (t.id = sub.contract_id)
Filter: (CASE WHEN (cancel.purchase_contract_id IS NULL) THEN t.amount ELSE '0'::numeric END < COALESCE(sub.amount, '0'::numeric))
-> Hash Left Join (cost=115.80..251.21 rows=249 width=14)
Hash Cond: (t.id = cancel.purchase_contract_id)
-> Seq Scan on tbl_purchase_contracts t (cost=0.00..134.76 rows=249 width=10)
Filter: ((NOT is_draft) AND (NOT is_refuse) AND (id IS NOT NULL) AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (cost_type = 1))
-> Hash (cost=115.78..115.78 rows=1 width=4)
-> Subquery Scan on cancel (cost=115.76..115.78 rows=1 width=4)
-> GroupAggregate (cost=115.76..115.77 rows=1 width=12)
Group Key: t_1.purchase_contract_id
-> Sort (cost=115.76..115.76 rows=1 width=4)
Sort Key: t_1.purchase_contract_id
-> Seq Scan on tbl_erp_purchase_orders t_1 (cost=0.00..115.75 rows=1 width=4)
Filter: ((cancel_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (cancel_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 8))
-> Hash (cost=644.36..644.36 rows=200 width=36)
-> Subquery Scan on sub (cost=639.86..644.36 rows=200 width=36)
-> HashAggregate (cost=639.86..642.36 rows=200 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=112.45..637.48 rows=477 width=21)
-> Subquery Scan on "*SELECT* 1" (cost=112.45..162.75 rows=271 width=10)
-> Hash Join (cost=112.45..160.04 rows=271 width=106)
Hash Cond: (r.erp_payment_ticket_id = t_2.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..41.01 rows=2501 width=14)
-> Hash (cost=109.27..109.27 rows=255 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_2 (cost=0.00..109.27 rows=255 width=4)
Filter: (is_payment_settle AND (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 2" (cost=1.18..4.05 rows=7 width=36)
-> Hash Join (cost=1.18..3.98 rows=7 width=132)
Hash Cond: (t_3.id = r_1.erp_other_income_ticket_id)
-> Seq Scan on tbl_erp_other_income_tickets t_3 (cost=0.00..2.60 rows=35 width=4)
Filter: ((bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.08..1.08 rows=8 width=40)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.08 rows=8 width=40)
-> Subquery Scan on "*SELECT* 3" (cost=0.29..323.36 rows=6 width=36)
-> Nested Loop (cost=0.29..323.30 rows=6 width=132)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.45 rows=45 width=40)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_4 (cost=0.29..7.15 rows=1 width=4)
Index Cond: (id = r_2.erp_cash_cost_ticket_id)
Filter: (is_payment_settle AND (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 4" (cost=103.33..147.32 rows=193 width=36)
-> Hash Join (cost=103.33..145.39 rows=193 width=132)
Hash Cond: (r_3.erp_collect_ticket_id = t_5.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..35.83 rows=2183 width=14)
-> Hash (cost=100.91..100.91 rows=194 width=4)
-> Seq Scan on tbl_erp_collect_tickets t_5 (cost=0.00..100.91 rows=194 width=4)
Filter: ((bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))