737
Views
12
Comments
Solved
How to get comma separated string using calculated column in Aggregate?
Question

how to get comma separated value using aggregate?


Screenshot35.png
2021-07-14 09-27-33
Luís Cardoso
Solution

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}
2020-01-08 12-23-57
Jitender Gaur

Luís Cardoso wrote:

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}


Hi Ranjeet, 


As per given solution by Luís Cardoso is working fine. Just add on to your query, Add distinct keyword with select and do also join with Role and UserRole table outside the STUFF column so after that those User will be filter who has not any role in application.   

 

2022-02-01 04-03-06
Ranjeet Singh

Jitender Gaur wrote:

Luís Cardoso wrote:

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}


Hi Ranjeet, 


As per given solution by Luís Cardoso is working fine. Just add on to your query, Add distinct keyword with select and do also join with Role and UserRole table outside the STUFF column so after that those User will be filter who has not any role in application.   

 

Hello Jitendra

Thank you so much for your valuable suggestion.


2022-02-01 04-03-06
Ranjeet Singh

Luís Cardoso wrote:

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}

Hello Luís Cardoso

Thank you so much for your valuable answer.


2019-08-08 09-29-47
Dharnendra Shah

Ranjeet Singh wrote:

how to get comma separated value using aggregate?

Hi Ranjeet,

Easiest solution would be go with SQL and use the 'COALESCE ' function to have comma separate values within SQL Query


2021-07-14 09-27-33
Luís Cardoso

Hi,


You need to add a new atrribute (last column on fields list):



On the Value of the Attribute you can select the fields and add the comma:

Please see this example:


Hope it helps.

PS: If you want to do like this example you don't need to change to SQL

BR,

Luís

2022-02-01 04-03-06
Ranjeet Singh

Luís Cardoso wrote:

Hi,


You need to add a new atrribute (last column on fields list):



On the Value of the Attribute you can select the fields and add the comma:

Please see this example:


Hope it helps.

PS: If you want to do like this example you don't need to change to SQL

BR,

Luís

Thank you for answer but for my case user can have multiple roles than how can i show multiple roles in single column.


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Ranjeet,

It depends on what you want. Can you give an example of the desired output? Why do you need it comma-seperated?

2022-02-01 04-03-06
Ranjeet Singh

Kilian Hekhuis wrote:

Hi Ranjeet,

It depends on what you want. Can you give an example of the desired output? Why do you need it comma-seperated?

Hi @Kilian Hekhuis

Output should be like :

UserName          Roles

mary jane            HR,Manager




2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Ranjeet,

That's something that's not solvable by a query alone, as far as my knowledge goes. If you want to show output on the screen, you could use a Web Block that has the user Id as input, that performs a query and concatenates the roles (e.g. by using String_Join from the Text Extension). But that will of course increase the number of queries. Alternatively, you could use your Aggregate and then "hand build" the desired output by looping over the results and create a new List on the fly.

2021-07-14 09-27-33
Luís Cardoso

You can do a SQL to get the Roles separeted by comma with this statment:


SELECT STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r

        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles

2021-07-14 09-27-33
Luís Cardoso
Solution

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}
2020-01-08 12-23-57
Jitender Gaur

Luís Cardoso wrote:

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}


Hi Ranjeet, 


As per given solution by Luís Cardoso is working fine. Just add on to your query, Add distinct keyword with select and do also join with Role and UserRole table outside the STUFF column so after that those User will be filter who has not any role in application.   

 

2022-02-01 04-03-06
Ranjeet Singh

Jitender Gaur wrote:

Luís Cardoso wrote:

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}


Hi Ranjeet, 


As per given solution by Luís Cardoso is working fine. Just add on to your query, Add distinct keyword with select and do also join with Role and UserRole table outside the STUFF column so after that those User will be filter who has not any role in application.   

 

Hello Jitendra

Thank you so much for your valuable suggestion.


2022-02-01 04-03-06
Ranjeet Singh

Luís Cardoso wrote:

Ranjeet Singh  you can try this SQL:

SELECT {User}.[Name],STUFF

(

    (

        SELECT ',' + r.Name

        FROM {Role} r
        inner join {UserRole} on {UserRole}.[Role_Id] = r.[Id]
        inner join {User} u on u.[id] = {UserRole}.[User_Id] 
        where u.[id] = {User}.[Id]
        ORDER BY r.Name FOR XML PATH('')

    ),

     1, 1, ''

) AS Roles
from {User}

Hello Luís Cardoso

Thank you so much for your valuable answer.


2026-03-20 01-28-51
Saugat Biswas

Hi Ranjeet,


I fully agree with Luis Cardoso's solution. 


However, if you want to bypass the complex SQL, then

Get the aggregate of roles list


And in your action perform a string_join passing the list as parameter and comma (,) as separator


Regards,

Saugat


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.