QUERY PLAN
Limit (cost=4308.35..4308.37 rows=10 width=278)
-> Sort (cost=4308.35..4313.34 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=4015.71..4265.17 rows=1998 width=278)
Hash Cond: (c.client_class = cc.id)
-> Hash Left Join (cost=4014.62..4165.58 rows=1998 width=202)
Hash Cond: (c.id = wl.client_id)
-> Hash Left Join (cost=2070.88..2216.59 rows=1998 width=194)
Hash Cond: (c.sales_rep = sr.id)
-> Hash Left Join (cost=2061.64..2202.02 rows=1998 width=189)
Hash Cond: (c.id = prev.client_id)
-> Hash Join (cost=1116.41..1251.53 rows=1998 width=125)
Hash Cond: (c_1.id = c.id)
-> HashAggregate (cost=982.45..1092.34 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=771.75..917.52 rows=1998 width=260)
Hash Cond: (c_1.id = esgs.client_id)
-> Hash Left Join (cost=743.50..884.01 rows=1998 width=228)
Hash Cond: (c_1.id = eccrt.client_id)
-> Hash Left Join (cost=742.27..877.53 rows=1998 width=196)
Hash Cond: (c_1.id = ecct.client_id)
-> Hash Left Join (cost=210.78..340.79 rows=1998 width=164)
Hash Cond: (c_1.id = ect2.client_id)
-> Hash Left Join (cost=35.37..160.11 rows=1998 width=132)
Hash Cond: (c_1.id = ect.client_id)
-> Hash Left Join (cost=32.20..151.69 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=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=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=920.26..920.26 rows=1998 width=68)
-> Subquery Scan on prev (cost=800.38..920.26 rows=1998 width=68)
-> HashAggregate (cost=800.38..900.28 rows=1998 width=68)
Group Key: c_2.id
-> Hash Left Join (cost=599.67..745.44 rows=1998 width=260)
Hash Cond: (c_2.id = esgs_1.client_id)
-> Hash Left Join (cost=571.42..711.93 rows=1998 width=228)
Hash Cond: (c_2.id = eccrt_1.client_id)
-> Hash Left Join (cost=570.19..705.45 rows=1998 width=196)
Hash Cond: (c_2.id = ecct_1.client_id)
-> Hash Left Join (cost=38.85..168.86 rows=1998 width=164)
Hash Cond: (c_2.id = ect2_1.client_id)
-> Hash Left Join (cost=27.46..152.21 rows=1998 width=132)
Hash Cond: (c_2.id = ect_1.client_id)
-> Hash Left Join (cost=24.37..143.87 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=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=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)