QUERY PLAN
Aggregate (cost=29.00..29.01 rows=1 width=8)
-> Unique (cost=28.73..28.87 rows=11 width=22)
-> Sort (cost=28.73..28.76 rows=11 width=22)
Sort Key: c.id, c.client_class, cc.notice_days, ((date_part('epoch'::text, (now() - max(sl.end_at))) / '86400'::double precision))
-> GroupAggregate (cost=28.18..28.54 rows=11 width=22)
Group Key: c.id, cc.id
Filter: ((date_part('epoch'::text, (now() - max(sl.end_at))) / '86400'::double precision) > (cc.notice_days)::double precision)
-> Sort (cost=28.18..28.21 rows=11 width=22)
Sort Key: c.id, cc.id
-> Nested Loop Left Join (cost=4.31..27.99 rows=11 width=22)
-> Nested Loop Left Join (cost=0.14..18.48 rows=1 width=14)
-> Seq Scan on tbl_clients c (cost=0.00..10.25 rows=1 width=8)
Filter: ((NOT deleted) AND (sales_rep = ANY ('{5493,5493}'::integer[])))
-> Index Scan using tbl_client_classes_pkey on tbl_client_classes cc (cost=0.14..8.16 rows=1 width=6)
Index Cond: (id = c.client_class)
-> Bitmap Heap Scan on tbl_work_logs sl (cost=4.16..9.50 rows=1 width=12)
Recheck Cond: (client_id = c.id)
Filter: (NOT is_draft)
-> Bitmap Index Scan on tbl_work_logs_client_id_idx (cost=0.00..4.16 rows=2 width=0)
Index Cond: (client_id = c.id)