QUERY PLAN
Aggregate (cost=727.87..727.88 rows=1 width=32)
-> Hash Left Join (cost=638.88..727.58 rows=56 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.20 rows=167 width=14)
Hash Cond: (t.id = cancel.purchase_contract_id)
-> Seq Scan on tbl_purchase_contracts t (cost=0.00..87.83 rows=167 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 <= '2025-12-31 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 <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 8))
-> Hash (cost=557.44..557.44 rows=200 width=36)
-> Subquery Scan on sub (cost=552.94..557.44 rows=200 width=36)
-> HashAggregate (cost=552.94..555.44 rows=200 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=76.53..551.30 rows=329 width=21)
-> Subquery Scan on "*SELECT* 1" (cost=76.53..124.19 rows=179 width=10)
-> Hash Join (cost=76.53..122.40 rows=179 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=178 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_2 (cost=0.00..74.31 rows=178 width=4)
Filter: (is_payment_settle AND (bank_settle_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 2" (cost=1.25..3.11 rows=7 width=10)
-> Hash Join (cost=1.25..3.04 rows=7 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=25 width=4)
Filter: ((bank_settle_at <= '2025-12-31 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.89 rows=5 width=9)
-> Nested Loop (cost=0.29..309.84 rows=5 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 <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 4" (cost=70.66..114.10 rows=138 width=36)
-> Hash Join (cost=70.66..112.72 rows=138 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=138 width=4)
-> Seq Scan on tbl_erp_collect_tickets t_5 (cost=0.00..68.94 rows=138 width=4)
Filter: ((bank_settle_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))