I am trying to combine UserName field into a comma separated string if the Name is the same. Current output:
Since Name = Admin has 4 users linked to it, I am trying to display as as admin@insi.co.za, miguel.reece8@gmail.com, mmm@test.com, test@test.com instead of it being in separate rows.
SQL query
SELECT DISTINCT
Name,
a3.UserName
FROM
PromotionTracker.dbo.AspNetRoles a1
JOIN
PromotionTracker.dbo.AspNetUserRoles a2
ON a1.Id = a2.RoleId
JOIN
PromotionTracker.dbo.AspNetUsers a3
ON a2.UserId = a3.Id
I tried using STUFF() function but it still gives me the same output not sure what is incorrect in the query.
Updated query with STUFF function
SELECT DISTINCT
Name,
STUFF((
SELECT DISTINCT
',' + a3.UserName FOR XML PATH('') ), 1, 1, '' )
FROM
PromotionTracker.dbo.AspNetRoles a1
JOIN
PromotionTracker.dbo.AspNetUserRoles a2
ON a1.Id = a2.RoleId
JOIN
PromotionTracker.dbo.AspNetUsers a3
ON a2.UserId = a3.Id