Skip to content
Advertisement

group by along with totals in the same query

I have the below query

select role,  count(*) as cases  from ( select
     CASE WHEN r.id = 30 THEN r.name ELSE r.name || ' ' || u.member_id END AS role
    from case_inventory ci, users u, roles r
    where ci.board_id = u.board_id and
          ci.assigned_to = u.io_id and 
          u.role_id = r.id
          and ci.case_id = 40) 
    group by role;

Output is :

  Role          Cases
  President      1
  Student Member 2  

I want the totals in the same query. How should I go forward?

  Role          Cases
  President      1
  Student Member 2  
  Totals         3

Advertisement

Answer

You can simply rewrire your query by adding rollup in the query:

select nvl(role, 'Totals') role,  count(*) as cases  from ( select
     CASE WHEN r.id = 30 THEN r.name ELSE r.name || ' ' || u.member_id END AS role
    from case_inventory ci, users u, roles r
    where ci.board_id = u.board_id and
          ci.assigned_to = u.io_id and 
          u.role_id = r.id
          and ci.case_id = 40) 
    group by Rollup(role);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement