Question

I have a table like below:

id name role1 role2 role3
-------------------------
1  John   y    n      y
2  Pete   n    y      y
3  Den    n    y      y
4  Mat    y    n      n

After I filter the table by using role1='Y', I lost Pete and Den.

How can I use the analysis to build a table like below:

       Count (Y)
Role1    3
Role2    2
Role3    3

I tried everything. Please help

Thanks

Was it helpful?

Solution

If your database is Oracle 11g or later you can use the unpivot clause

SELECT usr_role,
  COUNT(*) role_count
FROM
  (SELECT  * 
     FROM table_name 
     UNPIVOT (hasRole FOR usr_role IN (role1,role2,role3))
  WHERE hasRole = 'y'
  )
GROUP BY usr_role ;

This returns:

USR_ROLE  ROLE_COUNT
ROLE3      3
ROLE1      2
ROLE2      2

You could use this query as an opaque view in the RPD

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top