QUERY PLAN
Aggregate (cost=732.47..732.48 rows=1 width=32)
-> Hash Left Join (cost=642.88..732.04 rows=84 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=78.94..167.43 rows=253 width=14)
Hash Cond: (t.id = cancel.purchase_contract_id)
-> Seq Scan on tbl_purchase_contracts t (cost=0.00..87.83 rows=253 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=78.92..78.92 rows=1 width=4)
-> Subquery Scan on cancel (cost=78.90..78.92 rows=1 width=4)
-> GroupAggregate (cost=78.90..78.91 rows=1 width=12)
Group Key: t_1.purchase_contract_id
-> Sort (cost=78.90..78.90 rows=1 width=4)
Sort Key: t_1.purchase_contract_id
-> Seq Scan on tbl_erp_purchase_orders t_1 (cost=0.00..78.89 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=561.45..561.45 rows=200 width=36)
-> Subquery Scan on sub (cost=556.95..561.45 rows=200 width=36)
-> HashAggregate (cost=556.95..559.45 rows=200 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=77.51..554.60 rows=469 width=21)
-> Subquery Scan on "*SELECT* 1" (cost=77.51..125.95 rows=257 width=10)
-> Hash Join (cost=77.51..123.38 rows=257 width=106)
Hash Cond: (r.erp_payment_ticket_id = t_2.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..39.65 rows=2365 width=14)
-> Hash (cost=74.31..74.31 rows=256 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_2 (cost=0.00..74.31 rows=256 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.25..3.20 rows=9 width=10)
-> Hash Join (cost=1.25..3.11 rows=9 width=106)
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..1.60 rows=34 width=4)
Filter: ((bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.11..1.11 rows=11 width=14)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.11 rows=11 width=14)
-> Subquery Scan on "*SELECT* 3" (cost=0.29..309.91 rows=7 width=9)
-> Nested Loop (cost=0.29..309.84 rows=7 width=105)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.53 rows=53 width=13)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_4 (cost=0.29..5.82 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=71.39..115.55 rows=196 width=36)
-> Hash Join (cost=71.39..113.59 rows=196 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.94 rows=2194 width=14)
-> Hash (cost=68.94..68.94 rows=196 width=4)
-> Seq Scan on tbl_erp_collect_tickets t_5 (cost=0.00..68.94 rows=196 width=4)
Filter: ((bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))