QUERY PLAN
Aggregate (cost=61.71..61.72 rows=1 width=32)
-> Append (cost=38.06..61.71 rows=2 width=32)
-> Aggregate (cost=38.06..38.07 rows=1 width=32)
-> Nested Loop Left Join (cost=2.67..38.03 rows=9 width=5)
Filter: ((NOT (hashed SubPlan 1)) OR (t.sales_contract_id IS NULL))
-> Hash Join (cost=1.32..5.25 rows=9 width=9)
Hash Cond: (t.id = am.erp_other_income_ticket_id)
-> Seq Scan on tbl_erp_other_income_tickets t (cost=0.00..3.40 rows=51 width=8)
Filter: ((created_at >= '2025-06-27 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.14..1.14 rows=14 width=9)
-> Seq Scan on tbl_erp_other_income_ticket_handlers am (cost=0.00..1.14 rows=14 width=9)
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc (cost=0.28..3.51 rows=1 width=8)
Index Cond: (id = t.sales_contract_id)
SubPlan 1
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_3.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_3.project_id
-> Seq Scan on tbl_erp_profit_distributes t_3 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=23.61..23.62 rows=1 width=32)
-> Nested Loop (cost=17.72..23.60 rows=1 width=11)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=17.72..22.42 rows=1 width=10)
Join Filter: (sc_1.from_project = pd.project_id)
-> Hash Join (cost=17.72..21.33 rows=1 width=14)
Hash Cond: (t_1.sales_contract_id = sc_1.id)
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..3.40 rows=51 width=10)
Filter: ((created_at >= '2025-06-27 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=17.70..17.70 rows=2 width=12)
-> Nested Loop (cost=1.34..17.70 rows=2 width=12)
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_2.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_2.project_id
-> Seq Scan on tbl_erp_profit_distributes t_2 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Index Scan using tbl_sales_contracts_from_project_idx on tbl_sales_contracts sc_1 (cost=0.28..8.29 rows=1 width=8)
Index Cond: (from_project = t_2.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd (cost=0.00..1.04 rows=4 width=8)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_1 (cost=0.00..1.08 rows=8 width=9)