QUERY PLAN
Limit (cost=3821.80..3821.82 rows=10 width=278)
-> Sort (cost=3821.80..3826.79 rows=1998 width=278)
Sort Key: (COALESCE(((COALESCE(sum(o.outbound_amount), '0'::numeric) + COALESCE(sum(eoi.amount), '0'::numeric))), '0'::numeric)) DESC NULLS LAST
-> Hash Left Join (cost=3529.16..3778.62 rows=1998 width=278)
Hash Cond: (c.client_class = cc.id)
-> Hash Left Join (cost=3528.07..3679.03 rows=1998 width=202)
Hash Cond: (c.id = wl.client_id)
-> Hash Left Join (cost=1584.34..1730.04 rows=1998 width=194)
Hash Cond: (c.sales_rep = sr.id)
-> Hash Left Join (cost=1575.10..1715.48 rows=1998 width=189)
Hash Cond: (c.id = prev.client_id)
-> Hash Join (cost=830.37..965.50 rows=1998 width=125)
Hash Cond: (c_1.id = c.id)
-> HashAggregate (cost=696.42..806.31 rows=1998 width=68)
Group Key: c_1.id
Filter: ((COALESCE(sum(o.outbound_amount), '0'::numeric) + COALESCE(sum(eoi.amount), '0'::numeric)) > '0'::numeric)
-> Hash Left Join (cost=485.71..631.48 rows=1998 width=260)
Hash Cond: (c_1.id = esgs.client_id)
-> Hash Left Join (cost=460.08..600.59 rows=1998 width=228)
Hash Cond: (c_1.id = eccrt.client_id)
-> Hash Left Join (cost=458.85..594.11 rows=1998 width=196)
Hash Cond: (c_1.id = ecct.client_id)
-> Hash Left Join (cost=127.91..257.91 rows=1998 width=164)
Hash Cond: (c_1.id = ect2.client_id)
-> Hash Left Join (cost=28.45..153.20 rows=1998 width=132)
Hash Cond: (c_1.id = ect.client_id)
-> Hash Left Join (cost=25.36..144.85 rows=1998 width=100)
Hash Cond: (c_1.id = eoi.client_id)
-> Hash Left Join (cost=23.61..137.84 rows=1998 width=68)
Hash Cond: (c_1.id = o.client_id)
-> Seq Scan on tbl_clients c_1 (cost=0.00..108.98 rows=1998 width=4)
Filter: (NOT deleted)
-> Hash (cost=23.19..23.19 rows=33 width=68)
-> Subquery Scan on o (cost=22.37..23.19 rows=33 width=68)
-> HashAggregate (cost=22.37..22.86 rows=33 width=68)
Group Key: t.client_id
-> Hash Right Join (cost=7.17..21.33 rows=139 width=14)
Hash Cond: (eipss.erp_sales_outbound_id = t.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.65 rows=565 width=14)
-> Hash (cost=6.70..6.70 rows=38 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t (cost=0.00..6.70 rows=38 width=8)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.74..1.74 rows=1 width=36)
-> Subquery Scan on eoi (cost=1.71..1.74 rows=1 width=36)
-> GroupAggregate (cost=1.71..1.73 rows=1 width=36)
Group Key: t_1.client_id
-> Sort (cost=1.71..1.72 rows=1 width=10)
Sort Key: t_1.client_id
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..1.70 rows=1 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=3.07..3.07 rows=1 width=36)
-> Subquery Scan on ect (cost=3.04..3.07 rows=1 width=36)
-> GroupAggregate (cost=3.04..3.06 rows=1 width=36)
Group Key: t_2.client_id
-> Sort (cost=3.04..3.05 rows=1 width=9)
Sort Key: t_2.client_id
-> Seq Scan on tbl_erp_cost_tickets t_2 (cost=0.00..3.03 rows=1 width=9)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=99.35..99.35 rows=9 width=36)
-> Subquery Scan on ect2 (cost=99.08..99.35 rows=9 width=36)
-> GroupAggregate (cost=99.08..99.26 rows=9 width=36)
Group Key: con.client_id
-> Sort (cost=99.08..99.10 rows=9 width=9)
Sort Key: con.client_id
-> Nested Loop (cost=0.28..98.94 rows=9 width=9)
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=0.00..3.03 rows=13 width=9)
Filter: ((created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_contacts_pkey on tbl_contacts con (cost=0.28..7.38 rows=1 width=8)
Index Cond: (id = t_3.contact_id)
Filter: (client_id IS NOT NULL)
-> Hash (cost=328.93..328.93 rows=161 width=36)
-> Subquery Scan on ecct (cost=325.30..328.93 rows=161 width=36)
-> HashAggregate (cost=325.30..327.31 rows=161 width=36)
Group Key: t_4.client_id
-> Seq Scan on tbl_erp_cash_cost_tickets t_4 (cost=0.00..324.31 rows=198 width=9)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.22..1.22 rows=1 width=36)
-> Subquery Scan on eccrt (cost=1.20..1.22 rows=1 width=36)
-> HashAggregate (cost=1.20..1.21 rows=1 width=36)
Group Key: t_5.client_id
-> Seq Scan on tbl_erp_cost_convert_tickets t_5 (cost=0.00..1.19 rows=1 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=25.01..25.01 rows=50 width=36)
-> Subquery Scan on esgs (cost=23.88..25.01 rows=50 width=36)
-> HashAggregate (cost=23.88..24.51 rows=50 width=36)
Group Key: t_6.client_id
-> Hash Right Join (cost=6.03..20.17 rows=495 width=14)
Hash Cond: (eipss_1.erp_sales_gift_id = t_6.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_1 (cost=0.00..12.65 rows=565 width=14)
-> Hash (cost=4.61..4.61 rows=113 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_6 (cost=0.00..4.61 rows=113 width=8)
Filter: ((client_id IS NOT NULL) AND (status = 2))
-> Hash (cost=108.98..108.98 rows=1998 width=61)
-> Seq Scan on tbl_clients c (cost=0.00..108.98 rows=1998 width=61)
Filter: (NOT deleted)
-> Hash (cost=719.75..719.75 rows=1998 width=68)
-> Subquery Scan on prev (cost=599.87..719.75 rows=1998 width=68)
-> HashAggregate (cost=599.87..699.77 rows=1998 width=68)
Group Key: c_2.id
-> Hash Left Join (cost=399.15..544.92 rows=1998 width=260)
Hash Cond: (c_2.id = esgs_1.client_id)
-> Hash Left Join (cost=373.52..514.04 rows=1998 width=228)
Hash Cond: (c_2.id = eccrt_1.client_id)
-> Hash Left Join (cost=372.29..507.55 rows=1998 width=196)
Hash Cond: (c_2.id = ecct_1.client_id)
-> Hash Left Join (cost=37.22..167.22 rows=1998 width=164)
Hash Cond: (c_2.id = ect2_1.client_id)
-> Hash Left Join (cost=25.82..150.57 rows=1998 width=132)
Hash Cond: (c_2.id = ect_1.client_id)
-> Hash Left Join (cost=22.74..142.23 rows=1998 width=100)
Hash Cond: (c_2.id = eoi_1.client_id)
-> Hash Left Join (cost=20.98..135.22 rows=1998 width=68)
Hash Cond: (c_2.id = o_1.client_id)
-> Seq Scan on tbl_clients c_2 (cost=0.00..108.98 rows=1998 width=4)
Filter: (NOT deleted)
-> Hash (cost=20.97..20.97 rows=1 width=68)
-> Subquery Scan on o_1 (cost=20.90..20.97 rows=1 width=68)
-> GroupAggregate (cost=20.90..20.96 rows=1 width=68)
Group Key: t_7.client_id
-> Sort (cost=20.90..20.91 rows=4 width=14)
Sort Key: t_7.client_id
-> Hash Right Join (cost=6.71..20.86 rows=4 width=14)
Hash Cond: (eipss_2.erp_sales_outbound_id = t_7.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_2 (cost=0.00..12.65 rows=565 width=14)
-> Hash (cost=6.70..6.70 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_7 (cost=0.00..6.70 rows=1 width=8)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2025-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-06-18 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.74..1.74 rows=1 width=36)
-> Subquery Scan on eoi_1 (cost=1.71..1.74 rows=1 width=36)
-> GroupAggregate (cost=1.71..1.73 rows=1 width=36)
Group Key: t_8.client_id
-> Sort (cost=1.71..1.72 rows=1 width=10)
Sort Key: t_8.client_id
-> Seq Scan on tbl_erp_other_income_tickets t_8 (cost=0.00..1.70 rows=1 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2025-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-06-18 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=3.07..3.07 rows=1 width=36)
-> Subquery Scan on ect_1 (cost=3.04..3.07 rows=1 width=36)
-> GroupAggregate (cost=3.04..3.06 rows=1 width=36)
Group Key: t_9.client_id
-> Sort (cost=3.04..3.05 rows=1 width=9)
Sort Key: t_9.client_id
-> Seq Scan on tbl_erp_cost_tickets t_9 (cost=0.00..3.03 rows=1 width=9)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2025-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-06-18 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=11.38..11.38 rows=1 width=36)
-> Subquery Scan on ect2_1 (cost=11.35..11.38 rows=1 width=36)
-> GroupAggregate (cost=11.35..11.37 rows=1 width=36)
Group Key: con_1.client_id
-> Sort (cost=11.35..11.35 rows=1 width=9)
Sort Key: con_1.client_id
-> Nested Loop (cost=0.28..11.34 rows=1 width=9)
-> Seq Scan on tbl_erp_cost_tickets t_10 (cost=0.00..3.03 rows=1 width=9)
Filter: ((created_at >= '2025-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-06-18 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_contacts_pkey on tbl_contacts con_1 (cost=0.28..8.30 rows=1 width=8)
Index Cond: (id = t_10.contact_id)
Filter: (client_id IS NOT NULL)
-> Hash (cost=331.90..331.90 rows=254 width=36)
-> Subquery Scan on ecct_1 (cost=326.18..331.90 rows=254 width=36)
-> HashAggregate (cost=326.18..329.36 rows=254 width=36)
Group Key: t_11.client_id
-> Seq Scan on tbl_erp_cash_cost_tickets t_11 (cost=0.00..324.31 rows=374 width=9)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2025-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-06-18 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.22..1.22 rows=1 width=36)
-> Subquery Scan on eccrt_1 (cost=1.20..1.22 rows=1 width=36)
-> HashAggregate (cost=1.20..1.21 rows=1 width=36)
Group Key: t_12.client_id
-> Seq Scan on tbl_erp_cost_convert_tickets t_12 (cost=0.00..1.19 rows=1 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2025-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-06-18 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=25.01..25.01 rows=50 width=36)
-> Subquery Scan on esgs_1 (cost=23.88..25.01 rows=50 width=36)
-> HashAggregate (cost=23.88..24.51 rows=50 width=36)
Group Key: t_13.client_id
-> Hash Right Join (cost=6.03..20.17 rows=495 width=14)
Hash Cond: (eipss_3.erp_sales_gift_id = t_13.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_3 (cost=0.00..12.65 rows=565 width=14)
-> Hash (cost=4.61..4.61 rows=113 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_13 (cost=0.00..4.61 rows=113 width=8)
Filter: ((client_id IS NOT NULL) AND (status = 2))
-> Hash (cost=7.44..7.44 rows=144 width=13)
-> Seq Scan on tbl_users sr (cost=0.00..7.44 rows=144 width=13)
-> Hash (cost=1928.15..1928.15 rows=1247 width=12)
-> Subquery Scan on wl (cost=1890.74..1928.15 rows=1247 width=12)
-> HashAggregate (cost=1890.74..1915.68 rows=1247 width=12)
Group Key: tbl_work_logs.client_id
-> Seq Scan on tbl_work_logs (cost=0.00..1712.33 rows=35681 width=12)
Filter: (NOT is_draft)
-> Hash (cost=1.04..1.04 rows=4 width=6)
-> Seq Scan on tbl_client_classes cc (cost=0.00..1.04 rows=4 width=6)