Skip to content
Advertisement

Roll up multiple rows into one postgresql

There is a role table

id |created_at             |updated_at             |role           |source |username
7  |2019-12-01 01:38:31.965|2019-12-01 01:38:31.965|ROLE_ADMIN     |ACCESS |mammar
8  |2019-12-01 01:38:33.061|2019-12-01 01:38:33.061|ROLE_SUPERADMIN|ACCESS |mammar

How do I get the output as using SQL query :

username;role 
mammar , ROLE_ADMIN,ROLE_SUPERADMIN

Advertisement

Answer

Please try below query for your problem and let me know if still you are facing any issue.

    SELECT username
    ,STRING_AGG(ROLE, ',' ORDER BY ROLE) roles
FROM [Your_schema].ROLE
 GROUP BY username;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement