QUERY PLAN
Aggregate (cost=449.32..449.33 rows=1 width=32)
-> Hash Left Join (cost=321.51..449.09 rows=45 width=42)
Hash Cond: (t.id = sub.contract_id)
Filter: (CASE WHEN (cancel.sales_contract_id IS NULL) THEN t.amount ELSE '0'::numeric END >= COALESCE(sub.amount, '0'::numeric))
-> Hash Left Join (cost=100.06..227.29 rows=136 width=14)
Hash Cond: (t.id = cancel.sales_contract_id)
-> Seq Scan on tbl_sales_contracts t (cost=0.00..126.88 rows=136 width=10)
Filter: (build_by_project 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))
-> Hash (cost=100.05..100.05 rows=1 width=4)
-> Subquery Scan on cancel (cost=100.02..100.05 rows=1 width=4)
-> GroupAggregate (cost=100.02..100.04 rows=1 width=12)
Group Key: t_1.sales_contract_id
-> Sort (cost=100.02..100.03 rows=1 width=4)
Sort Key: t_1.sales_contract_id
-> Seq Scan on tbl_erp_sales_orders t_1 (cost=0.00..100.01 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=219.04..219.04 rows=192 width=36)
-> Subquery Scan on sub (cost=214.72..219.04 rows=192 width=36)
-> HashAggregate (cost=214.72..217.12 rows=192 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=103.33..213.76 rows=192 width=10)
-> Subquery Scan on "*SELECT* 1" (cost=103.33..146.73 rows=190 width=10)
-> Hash Join (cost=103.33..144.83 rows=190 width=106)
Hash Cond: (r.erp_collect_ticket_id = t_2.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r (cost=0.00..35.83 rows=2154 width=14)
Filter: (sales_contract_id IS NOT NULL)
-> Hash (cost=100.91..100.91 rows=194 width=4)
-> Seq Scan on tbl_erp_collect_tickets t_2 (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))
-> Subquery Scan on "*SELECT* 2" (cost=0.29..9.34 rows=1 width=36)
-> Nested Loop (cost=0.29..9.33 rows=1 width=132)
-> Seq Scan on tbl_erp_cash_cost_ticket_sales_contract_settles r_1 (cost=0.00..1.01 rows=1 width=40)
Filter: (sales_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_3 (cost=0.29..8.31 rows=1 width=4)
Index Cond: (id = r_1.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* 3" (cost=0.28..57.69 rows=1 width=36)
-> Nested Loop (cost=0.28..57.68 rows=1 width=132)
-> Seq Scan on tbl_erp_payment_ticket_settles r_2 (cost=0.00..41.01 rows=2 width=14)
Filter: (sales_contract_id IS NOT NULL)
-> Index Scan using tbl_erp_payment_tickets_pkey on tbl_erp_payment_tickets t_4 (cost=0.28..8.30 rows=1 width=4)
Index Cond: (id = r_2.erp_payment_ticket_id)
Filter: (is_payment_settle AND (bank_settle_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))