QUERY PLAN
Limit (cost=4311.81..4311.84 rows=10 width=278)
-> Sort (cost=4311.81..4316.81 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=4019.17..4268.63 rows=1998 width=278)
Hash Cond: (c.client_class = cc.id)
-> Hash Left Join (cost=4018.08..4169.04 rows=1998 width=202)
Hash Cond: (c.id = wl.client_id)
-> Hash Left Join (cost=2074.35..2220.05 rows=1998 width=194)
Hash Cond: (c.sales_rep = sr.id)
-> Hash Left Join (cost=2065.11..2205.49 rows=1998 width=189)
Hash Cond: (c.id = prev.client_id)
-> Hash Join (cost=1118.17..1253.30 rows=1998 width=125)
Hash Cond: (c_1.id = c.id)
-> HashAggregate (cost=984.22..1094.11 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=773.51..919.28 rows=1998 width=260)
Hash Cond: (c_1.id = esgs.client_id)
-> Hash Left Join (cost=745.26..885.78 rows=1998 width=228)
Hash Cond: (c_1.id = eccrt.client_id)
-> Hash Left Join (cost=744.03..879.29 rows=1998 width=196)
Hash Cond: (c_1.id = ecct.client_id)
-> Hash Left Join (cost=212.55..342.55 rows=1998 width=164)
Hash Cond: (c_1.id = ect2.client_id)
-> Hash Left Join (cost=37.13..161.88 rows=1998 width=132)
Hash Cond: (c_1.id = ect.client_id)
-> Hash Left Join (cost=33.97..153.46 rows=1998 width=100)
Hash Cond: (c_1.id = eoi.client_id)
-> Hash Left Join (cost=30.44..144.68 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=29.36..29.36 rows=87 width=68)
-> Subquery Scan on o (cost=27.18..29.36 rows=87 width=68)
-> HashAggregate (cost=27.18..28.49 rows=87 width=68)
Group Key: t.client_id
-> Hash Right Join (cost=10.05..24.20 rows=397 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=8.36..8.36 rows=135 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t (cost=0.00..8.36 rows=135 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-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=3.49..3.49 rows=3 width=36)
-> Subquery Scan on eoi (cost=3.42..3.49 rows=3 width=36)
-> HashAggregate (cost=3.42..3.46 rows=3 width=36)
Group Key: t_1.client_id
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..3.40 rows=4 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-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=3.12..3.12 rows=3 width=36)
-> Subquery Scan on ect (cost=3.06..3.12 rows=3 width=36)
-> HashAggregate (cost=3.06..3.10 rows=3 width=36)
Group Key: t_2.client_id
-> Seq Scan on tbl_erp_cost_tickets t_2 (cost=0.00..3.03 rows=5 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-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=175.07..175.07 rows=28 width=36)
-> Subquery Scan on ect2 (cost=174.23..175.07 rows=28 width=36)
-> GroupAggregate (cost=174.23..174.79 rows=28 width=36)
Group Key: con.client_id
-> Sort (cost=174.23..174.30 rows=28 width=9)
Sort Key: con.client_id
-> Hash Join (cost=3.52..173.56 rows=28 width=9)
Hash Cond: (con.id = t_3.contact_id)
-> Seq Scan on tbl_contacts con (cost=0.00..139.88 rows=3420 width=8)
Filter: (client_id IS NOT NULL)
-> Hash (cost=3.03..3.03 rows=39 width=9)
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=0.00..3.03 rows=39 width=9)
Filter: ((created_at >= '2025-01-01 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=526.63..526.63 rows=388 width=36)
-> Subquery Scan on ecct (cost=517.90..526.63 rows=388 width=36)
-> HashAggregate (cost=517.90..522.75 rows=388 width=36)
Group Key: t_4.client_id
-> Seq Scan on tbl_erp_cash_cost_tickets t_4 (cost=0.00..513.31 rows=918 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-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 >= '2025-01-01 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=27.41..27.41 rows=67 width=36)
-> Subquery Scan on esgs (cost=25.90..27.41 rows=67 width=36)
-> HashAggregate (cost=25.90..26.74 rows=67 width=36)
Group Key: t_6.client_id
-> Hash Right Join (cost=8.04..22.18 rows=496 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=6.15..6.15 rows=151 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_6 (cost=0.00..6.15 rows=151 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=921.96..921.96 rows=1998 width=68)
-> Subquery Scan on prev (cost=802.08..921.96 rows=1998 width=68)
-> HashAggregate (cost=802.08..901.98 rows=1998 width=68)
Group Key: c_2.id
-> Hash Left Join (cost=601.37..747.14 rows=1998 width=260)
Hash Cond: (c_2.id = esgs_1.client_id)
-> Hash Left Join (cost=573.12..713.63 rows=1998 width=228)
Hash Cond: (c_2.id = eccrt_1.client_id)
-> Hash Left Join (cost=571.89..707.15 rows=1998 width=196)
Hash Cond: (c_2.id = ecct_1.client_id)
-> Hash Left Join (cost=40.55..170.56 rows=1998 width=164)
Hash Cond: (c_2.id = ect2_1.client_id)
-> Hash Left Join (cost=29.16..153.91 rows=1998 width=132)
Hash Cond: (c_2.id = ect_1.client_id)
-> Hash Left Join (cost=26.07..145.57 rows=1998 width=100)
Hash Cond: (c_2.id = eoi_1.client_id)
-> Hash Left Join (cost=22.62..136.86 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=22.61..22.61 rows=1 width=68)
-> Subquery Scan on o_1 (cost=22.55..22.61 rows=1 width=68)
-> GroupAggregate (cost=22.55..22.60 rows=1 width=68)
Group Key: t_7.client_id
-> Sort (cost=22.55..22.56 rows=3 width=14)
Sort Key: t_7.client_id
-> Hash Right Join (cost=8.37..22.53 rows=3 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=8.36..8.36 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_7 (cost=0.00..8.36 rows=1 width=8)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2024-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2024-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=3.44..3.44 rows=1 width=36)
-> Subquery Scan on eoi_1 (cost=3.41..3.44 rows=1 width=36)
-> GroupAggregate (cost=3.41..3.43 rows=1 width=36)
Group Key: t_8.client_id
-> Sort (cost=3.41..3.42 rows=1 width=10)
Sort Key: t_8.client_id
-> Seq Scan on tbl_erp_other_income_tickets t_8 (cost=0.00..3.40 rows=1 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2024-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2024-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_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 >= '2024-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2024-12-31 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 >= '2024-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2024-12-31 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=526.51..526.51 rows=386 width=36)
-> Subquery Scan on ecct_1 (cost=517.82..526.51 rows=386 width=36)
-> HashAggregate (cost=517.82..522.65 rows=386 width=36)
Group Key: t_11.client_id
-> Seq Scan on tbl_erp_cash_cost_tickets t_11 (cost=0.00..513.31 rows=902 width=9)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2024-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2024-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_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 >= '2024-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2024-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=27.41..27.41 rows=67 width=36)
-> Subquery Scan on esgs_1 (cost=25.90..27.41 rows=67 width=36)
-> HashAggregate (cost=25.90..26.74 rows=67 width=36)
Group Key: t_13.client_id
-> Hash Right Join (cost=8.04..22.18 rows=496 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=6.15..6.15 rows=151 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_13 (cost=0.00..6.15 rows=151 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)