QUERY PLAN
Limit (cost=3920.79..3920.82 rows=10 width=278)
-> Sort (cost=3920.79..3925.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=3628.15..3877.62 rows=1998 width=278)
Hash Cond: (c.client_class = cc.id)
-> Hash Left Join (cost=3627.06..3778.02 rows=1998 width=202)
Hash Cond: (c.id = wl.client_id)
-> Hash Left Join (cost=1683.33..1829.03 rows=1998 width=194)
Hash Cond: (c.sales_rep = sr.id)
-> Hash Left Join (cost=1674.09..1814.47 rows=1998 width=189)
Hash Cond: (c.id = prev.client_id)
-> Hash Join (cost=922.11..1057.23 rows=1998 width=125)
Hash Cond: (c_1.id = c.id)
-> HashAggregate (cost=788.15..898.04 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=577.45..723.22 rows=1998 width=260)
Hash Cond: (c_1.id = esgs.client_id)
-> Hash Left Join (cost=551.81..692.33 rows=1998 width=228)
Hash Cond: (c_1.id = eccrt.client_id)
-> Hash Left Join (cost=550.58..685.84 rows=1998 width=196)
Hash Cond: (c_1.id = ecct.client_id)
-> Hash Left Join (cost=208.10..338.10 rows=1998 width=164)
Hash Cond: (c_1.id = ect2.client_id)
-> Hash Left Join (cost=32.68..157.43 rows=1998 width=132)
Hash Cond: (c_1.id = ect.client_id)
-> Hash Left Join (cost=29.52..149.01 rows=1998 width=100)
Hash Cond: (c_1.id = eoi.client_id)
-> Hash Left Join (cost=27.76..141.99 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=26.90..26.90 rows=69 width=68)
-> Subquery Scan on o (cost=25.17..26.90 rows=69 width=68)
-> HashAggregate (cost=25.17..26.21 rows=69 width=68)
Group Key: t.client_id
-> Hash Right Join (cost=8.04..22.20 rows=396 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=108 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t (cost=0.00..6.70 rows=108 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=1.75..1.75 rows=1 width=36)
-> Subquery Scan on eoi (cost=1.71..1.75 rows=1 width=36)
-> GroupAggregate (cost=1.71..1.74 rows=1 width=36)
Group Key: t_1.client_id
-> Sort (cost=1.71..1.72 rows=2 width=10)
Sort Key: t_1.client_id
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..1.70 rows=2 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=337.63..337.63 rows=388 width=36)
-> Subquery Scan on ecct (cost=328.90..337.63 rows=388 width=36)
-> HashAggregate (cost=328.90..333.75 rows=388 width=36)
Group Key: t_4.client_id
-> Seq Scan on tbl_erp_cash_cost_tickets t_4 (cost=0.00..324.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=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=727.01..727.01 rows=1998 width=68)
-> Subquery Scan on prev (cost=607.13..727.01 rows=1998 width=68)
-> HashAggregate (cost=607.13..707.03 rows=1998 width=68)
Group Key: c_2.id
-> Hash Left Join (cost=406.41..552.18 rows=1998 width=260)
Hash Cond: (c_2.id = esgs_1.client_id)
-> Hash Left Join (cost=380.78..521.30 rows=1998 width=228)
Hash Cond: (c_2.id = eccrt_1.client_id)
-> Hash Left Join (cost=379.55..514.81 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 >= '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.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 >= '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=337.51..337.51 rows=386 width=36)
-> Subquery Scan on ecct_1 (cost=328.82..337.51 rows=386 width=36)
-> HashAggregate (cost=328.82..333.65 rows=386 width=36)
Group Key: t_11.client_id
-> Seq Scan on tbl_erp_cash_cost_tickets t_11 (cost=0.00..324.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=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)