postgresql7.1 select issue_type, count(*) as n_issues from ec_customer_service_issues LEFT JOIN ec_cs_issue_type_map on (ec_customer_service_issues.issue_id=ec_cs_issue_type_map.issue_id) group by issue_type order by case when issue_type is null then 1 else 0 end $issue_type_decode select info_used, count(*) as n_actions from ec_customer_service_actions LEFT JOIN ec_cs_action_info_used_map on (ec_customer_service_actions.action_id = ec_cs_action_info_used_map.action_id) group by info_used order by case when info_used is null then 1 else 0 end $info_used_decode select interaction_originator, count(*) as n_interactions from ec_customer_serv_interactions group by interaction_originator order by case when interaction_originator='customer' then 0 when interaction_originator='rep' then 1 when interaction_originator='automatic' then 2 end , case when issue_type='[DoubleApos $issue_type]' then $issue_type_counter else $issue_type_counter end , case when info_used='[DoubleApos $info_used]' then $info_used_counter else $info_used_counter end