QUERY PLAN
Aggregate (cost=195.87..195.88 rows=1 width=32)
-> Nested Loop Left Join (cost=4.95..195.87 rows=1 width=6)
Join Filter: ((g.object_type = "*SELECT* 1".object_type) AND (g.object_id = "*SELECT* 1".object_id) AND (g.event_type = "*SELECT* 1".event_type) AND (g.event_id = "*SELECT* 1".event_id))
Filter: ((g.success_count IS NULL) OR (g.success_count = 0))
-> Append (cost=1.33..190.30 rows=2 width=22)
-> Subquery Scan on "*SELECT* 1" (cost=1.33..24.71 rows=1 width=21)
Filter: (("*SELECT* 1".object_type <> ALL ('{20,20018}'::integer[])) OR "*SELECT* 1".have_invoice OR (SubPlan 1))
-> Hash Join (cost=1.33..4.07 rows=1 width=177)
Hash Cond: (t.id = e.erp_other_payment_ticket_id)
-> Seq Scan on tbl_erp_other_payment_tickets t (cost=0.00..2.56 rows=44 width=4)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash (cost=1.32..1.32 rows=1 width=13)
-> Seq Scan on tbl_erp_other_payment_ticket_bank_ins e (cost=0.00..1.32 rows=1 width=13)
Filter: ((settle_at IS NOT NULL) AND (amount > '0'::numeric) AND (settle_at >= '2026-07-01 00:00:00+08'::timestamp with time zone) AND (settle_at <= '2026-07-31 23:59:59+08'::timestamp with time zone) AND (amount > '0'::numeric))
SubPlan 1
-> Nested Loop (cost=0.29..20.63 rows=1 width=0)
Join Filter: (r.taxonomy_id = tt.id)
-> Index Scan using tbl_term_taxonomy_relationships_object_id_term_id_idx on tbl_term_taxonomy_relationships r (cost=0.29..11.58 rows=1 width=4)
Index Cond: ((object_id = "*SELECT* 1".object_id) AND (term_id = 18))
Filter: ((("*SELECT* 1".object_type = 20) AND (object_type = 7)) OR (("*SELECT* 1".object_type = 20018) AND (object_type = 11)))
-> Seq Scan on tbl_term_taxonomies tt (cost=0.00..8.99 rows=5 width=4)
Filter: ((serial_no)::text = ANY ('{221106,640301,640302,640303,600704}'::text[]))
-> Subquery Scan on "*SELECT* 2" (cost=144.92..165.58 rows=1 width=22)
Filter: (("*SELECT* 2".object_type <> ALL ('{20,20018}'::integer[])) OR "*SELECT* 2".have_invoice OR (SubPlan 1))
-> GroupAggregate (cost=144.92..144.94 rows=1 width=117)
Group Key: s.id, t_1.id, p.bank_settle_at
-> Sort (cost=144.92..144.92 rows=1 width=63)
Sort Key: s.id, t_1.id, p.bank_settle_at
-> Nested Loop (cost=138.05..144.91 rows=1 width=63)
-> Hash Right Join (cost=137.91..144.74 rows=1 width=22)
Hash Cond: (bo.erp_payment_ticket_id = p.id)
-> Seq Scan on tbl_erp_payment_ticket_bank_outs bo (cost=0.00..5.78 rows=278 width=4)
-> Hash (cost=137.90..137.90 rows=1 width=26)
-> Hash Join (cost=80.21..137.90 rows=1 width=26)
Hash Cond: (s.erp_payment_ticket_id = p.id)
-> Seq Scan on tbl_erp_payment_ticket_settles s (cost=0.00..51.48 rows=2364 width=18)
Filter: ((amount > '0'::numeric) AND (amount > '0'::numeric))
-> Hash (cost=80.20..80.20 rows=1 width=12)
-> Seq Scan on tbl_erp_payment_tickets p (cost=0.00..80.20 rows=1 width=12)
Filter: (is_payment_settle AND (bank_settle_at IS NOT NULL) AND (status = ANY ('{2,5}'::integer[])) AND (bank_settle_at >= '2026-07-01 00:00:00+08'::timestamp with time zone) AND (bank_settle_at <= '2026-07-31 23:59:59+08'::timestamp with time zone))
-> Index Scan using tbl_erp_other_payment_tickets_pkey on tbl_erp_other_payment_tickets t_1 (cost=0.14..0.16 rows=1 width=45)
Index Cond: (id = s.erp_other_payment_ticket_id)
-> Materialize (cost=3.62..4.33 rows=26 width=24)
-> Subquery Scan on g (cost=3.62..4.21 rows=26 width=24)
-> HashAggregate (cost=3.62..3.94 rows=26 width=36)
Group Key: v.object_type, v.object_id, COALESCE((v.event_type)::integer, 0), COALESCE(v.event_id, 0)
-> Seq Scan on tbl_voucher_gen_items v (cost=0.00..2.81 rows=54 width=18)
Filter: (ledger_id = 1)