1

I have an Oracle database, and I need a query for reporting purposes. In my table, I have action_types and the date of the action. I want to count up the number of actions in certain groups and show the most recent action of that type.

So what I want is something like

   Group               Number             Date
 Administrative          3              01/02/2015
 User                    1              01/03/2015  
 View-Only               2              01/04/2015  

The problem is that each of those groups include multiple action_types, so the table itself looks like

 Action_Type              Date
    01                 01/02/2015
    04                 01/03/2015
    02                 01/02/2015
    03                 01/02/2015
    05                 01/03/2015
    06                 01/04/2015

Where actions 01, 02, 03 are all administrative actions, 04 is a user action, and 05 and 06 are view-only actions.

I can write a query that will return a single row of what I want- the number and most recent date of a single group, but I want a query that will have all the groups together, with each one having its own row as above. Is this possible?

3
  • Is there another table that says that 01, 02 and 03 are administrative etc.?
    – Alex Poole
    Aug 11, 2015 at 15:46
  • Unfortunately no, there is no table for that. These actions are only grouped for reporting purposes and these groups aren't actually used in the code or database.
    – DFL
    Aug 11, 2015 at 15:50
  • OK, then what Gordon said *8-)
    – Alex Poole
    Aug 11, 2015 at 15:53

1 Answer 1

5

I think you just want a case statement for grouping by:

select (case when Action_Type in ('01', '02', '03') then 'Administrative'
             when Action_Type in ('04') then 'User'
             when Action_Type in ('05', '06') then 'View-Only'
             else 'Unknown'
        end) as Action_Group, count(*), max(date)
from actions a
group by (case when Action_Type in ('01', '02', '03') then 'Administrative'
               when Action_Type in ('04') then 'User'
               when Action_Type in ('05', '06') then 'View-Only'
               else 'Unknown'
          end) ;

I would advise you, however, to create a reference table for action types that include the grouping that you want (if you don't already have such a table).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.