I have a very big query with multiple joins, i am some records but they are duplicated, there 3 columns which having different data
so i am trying to merge those all 3 in one row, with the values as comma separated
I used the CTE to get the table as:
;with cte(id,name,email,roles,country) as( select from mytable 4 joins and where clauses with data passed to it));
now i am doing this
select id,name,stuff((select email + ',' from cte FOR XML PATH ('')), 1, 1, '')) from cte group by id,name
but it is populating all the other rows where the data does not belong...
i have the results like this
ID name email roles campus
1 User1 user@gmail.com Admin Egypt
2 User1 user@gmail.com Moderator Egypt
3 User1 user@gmail.com Guest USA
4 User2 user2@gmail.com User Brazil
5 User2 user2@gmail.com Admin Cairo
6 User2 user2@gmail.com Dummy Namibia
and trying to build like this
ID name email roles campus
1 User1 user@gmail.com Admin,Moderator,Egypt Egypt,Egypt,USA
2 User2 user1@gmail.com User,Admin,Dummy Brazil,Cairo,Namibia