postgresql7.1 select * from (select tc.category_id, tc.name as category, count(tt.ticket_id) as total, sum(case when lower(state)='active' then 1 else 0 end) as active, sum(case when lower(state)='suspended' then 1 else 0 end) as suspended, sum(case when lower(state)='canceled' then 1 else 0 end) as canceled, sum(case when lower(state)='finished' then 1 else 0 end) as finished from ttracker_categories tc, ttracker_tickets tt, wf_cases wfc where tc.package_id = :package_id and tt.category_id = tc.category_id and tt.ticket_id = wfc.object_id [ad_dimensional_sql $dimensional] group by tc.category_id, tc.name union all select tc1.category_id, tc1.name as category, 0 as total, 0 as active, 0 as suspended, 0 as canceled, 0 as finished from ttracker_categories tc1 where tc1.package_id = :package_id and not exists (select tt1.ticket_id from ttracker_tickets tt1 where tt1.category_id = tc1.category_id [ad_dimensional_sql $dimensional])) as tasks [ad_order_by_from_sort_spec $orderby $table_def] creation_date + '365 days'::interval > now() creation_date + '90 days'::interval > now() creation_date + '30 days'::interval > now()