0

I am trying to return data in comma separated values with below query.

select u.employeeid,
       ( select name
         from   roles r
         where  r.id = ur.role_id
       ) userrole
from   users u,
       user_role ur
where  u.id in (select DISTINCT ur.user_id
                from user_role ur)
order by u.employeeid asc;

Current Output:

EMPLOYEEID USERRROLE
---------------------
1000    ROLE_SUPER_ADMIN
1000    ONBOARDING_CHECKER
1000    ROLE_SUPER_ADMIN
1000    ROLE_APPROVER
1000    ROLE_ONBORDING

Now when I execute the query, I want table to be like:

EMPLOYEEID USERRROLE
--------------------
1000       ROLE_SUPER_ADMIN,ONBOARDING_CHECKER,ROLE_SUPER_ADMIN,ROLE_APPROVER,ROLE_ONBORDING

2 Answers 2

1

Use LISTAGG (and ANSI joins rather than legacy comma joins and confusing sub-queries). What you probably want is:

SELECT u.employeeid,
       LISTAGG( r.name, ',' ) WITHIN GROUP ( ORDER BY r.name ) AS userrole
FROM   users u
       INNER JOIN user_role ur
       ON ( u.id = ur.user_id )
       LEFT OUTER JOIN roles r
       ON ( r.id = ur.role_id )
GROUP BY u.employeeid
ORDER BY u.employeeid asc;

Although I think what you have actually written would be:

SELECT u.employeeid,
       LISTAGG( r.name, ',' ) WITHIN GROUP ( ORDER BY r.name ) AS userrole
FROM   users u
       CROSS JOIN user_role ur
       LEFT OUTER JOIN roles r
       ON ( r.id = ur.role_id )
WHERE  EXISTS ( SELECT 1
                FROM   user_roles x
                WHERE  u.id = x.user_id )
GROUP BY u.employeeid
ORDER BY u.employeeid asc;
Sign up to request clarification or add additional context in comments.

7 Comments

getting 01489. 00000 - "result of string concatenation is too long" error
and also i want to get few more columns from users u table
@Durga A VARCHAR2 data type (which is what LISTAGG returns) can store up to 4000 characters. If your concatenated string is longer than that then you will get that error and your options are: (1) truncate the string to 4000 characters; (2) find a custom aggregation function that concatenates strings to a CLOB; (3) use XML to concatenate the value (but you won't have a comma separated string, you'll have XML); or (4) use the CAST and COLLECT functions to return a collection (array) of strings rather than a delimited string. There are solutions for all of those options on StackOverflow.
@Durga "i want to get few more columns from users u table" .... edit your question with a minimal reproducible example that includes: DDL statements for your table structures; DML statements for some example data in those tables that demonstrates the issue; and your expected output for that input. Just saying "i want more" is not constructive as we can't determine what "more" you want.
@Durga . . . Don't edit this question. MT0 has answered the question that you asked. If you have a new question, ask it as a new question.
|
0

this will do:

SELECT LISTAGG("USERRROLE", ',') WITHIN GROUP (ORDER BY "USERRROLE")
FROM Table1 group by "EMPLOYEEID";

check fiddle:http://sqlfiddle.com/#!4/d11c6/4

Thanks!!!!!!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.