0

I have the following query

select r.role_id, 
r.role_name, 
p.permission_name 
from t_user_role ur, t_role r, t_role_permission rp, t_permission p 
where ur.role_id=r.role_id 
and ur.role_id=rp.role_id 
and rp.permission_id=p.permission_id
and ur.contact_id=505;

which gives me the follwing result

enter image description here

I want to club the rows having similar row id to get the result like below:

ROLE_ID                   ROLE_NAME               PERMISSION_NAME   
100                       ops_center               List Dashboard
106                       oc_public                RunETN Activity Detail | Run Test View | .... and so on

Please Help

Taryn
  • 242,637
  • 56
  • 362
  • 405
Bhuvan
  • 2,209
  • 11
  • 33
  • 46
  • 1
    Please specify which DBMS do you use! Without that, you won't get an answer to this question.... (Though you seem to use SQL Developer, so very likely you are using **Oracle**) – ppeterka Nov 23 '12 at 12:02
  • take a look at oracle LISTAGG() analytical function (assuming Oracle) – Aditya Jain Nov 23 '12 at 12:02
  • probably duplicate of this? [http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005][1] [1]: http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-microsoft-sql-server-2005 – sayannayas Nov 23 '12 at 12:08
  • Ppeterka: I am using oracle – Bhuvan Nov 23 '12 at 14:47

1 Answers1

2

Based on your statement below, I am assuming you are using MySQL and have tried using GROUP_CONCAT():

I have tried using Group Contact but I am not able to make a query.

If you are using MySQL then you will apply GROUP_CONCAT() like this:

select r.role_id, 
  r.role_name, 
  group_concat(p.permission_name separator ' | ') permission_name
from t_user_role ur
inner join t_role r
  on ur.role_id=r.role_id 
inner join t_role_permission rp
  on ur.role_id=rp.role_id 
inner join t_permission p 
  on rp.permission_id=p.permission_id
where ur.contact_id=505
group by r.role_id, r.role_name;

Note: I changed the query to use ANSI JOIN syntax instead of a comma separated list of tables

In Oracle 11g, you can use LISTAGG():

select r.role_id, 
  r.role_name, 
  LISTAGG(p.permission_name, ' | ') WITHIN GROUP (ORDER BY r.role_id) permission_name
from t_user_role ur
inner join t_role r
  on ur.role_id=r.role_id 
inner join t_role_permission rp
  on ur.role_id=rp.role_id 
inner join t_permission p 
  on rp.permission_id=p.permission_id
where ur.contact_id=505
group by r.role_id, r.role_name;
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • Bluefeet: I am using oracle. Sorry I didnt knew Group_contact is in mysql and not in oracle – Bhuvan Nov 23 '12 at 14:49
  • @bhuvan what version of oracle? There is `LISTAGG` in 11g, see my edit. – Taryn Nov 23 '12 at 14:50
  • Oh yes. I am using oracle 11g. Havent explore LISTAGG yet. Will do it soon – Bhuvan Nov 23 '12 at 14:53
  • @bhuvan `LISTAGG` will be the best way (in my opinion) to do this, I updated my answer with a sample query and a link to the `LISTAGG` oracle doc – Taryn Nov 23 '12 at 14:55
  • Thanks BlueFeet... LISTAGG was the first thing i did on Monday morning and it worked like a charm. Thanks a lot :) – Bhuvan Nov 26 '12 at 06:15
  • If I want to make a query for oracle 10g then what would be the changes? Reason for asking is that one of our client is using oracle 10g. So I need to write a query for oracle 10g so that it works with oracle 11g version as well – Bhuvan Nov 26 '12 at 06:34
  • 1
    @bhuvan if you are not on 11g, then you should be able to use `wm_concat` or any of the methods described here -- http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php – Taryn Nov 26 '12 at 10:05