postgresql7.1 select t.ticket_id, a.name as area_name, case when t.priority=1 then 'High' else (case when t.priority=2 then 'Medium' else (case when t.priority=3 then 'Low' else t.priority::varchar end) end ) end as ticket_priority, t.status, s.sort_key, t.name as ticket_name, ttl_relative_date(coalesce(c.creation_date, t.last_modified)) as last_comment, case when deadline is null then null else to_char(deadline, 'Mon DD') end as ticket_deadline from ttl_va_tickets t left join ttl_v_comments_latest c using (ticket_id), ttl_status s, ttl_va_areas a where a.context_id = :package_id and a.area_id = t.area_id and t.status = s.status $user_id_clause $status_clause $area_clause $order_clause select * from ( select t.ticket_id, a.name as area_name, case when t.priority=1 then 'High' else (case when t.priority=2 then 'Medium' else (case when t.priority=3 then 'Low' else t.priority::varchar end) end ) end as ticket_priority, t.status, s.sort_key, t.name as ticket_name, ttl_relative_date(coalesce(c.creation_date, t.last_modified)) as last_comment, case when deadline is null then null else to_char(deadline, 'Mon DD') end as ticket_deadline from ttl_va_tickets t left join ttl_v_comments_latest c using (ticket_id), ttl_status s, ttl_va_areas a where a.context_id = :package_id and a.area_id = t.area_id and t.status = s.status $user_id_clause $status_clause $area_clause $order_clause ) paged_tickets where ticket_id in (CURRENT_PAGE_SET) order by deadline asc, coalesce(c.creation_date, t.last_modified) desc order by coalesce(c.creation_date, t.last_modified) desc order by lower(a.name) asc, priority asc, coalesce(c.creation_date, t.last_modified) desc order by lower(s.sort_key) asc, priority asc, coalesce(c.creation_date, t.last_modified) desc order by priority asc, coalesce(c.creation_date, t.last_modified) desc