CREATE TABLE #temp
(
ID INT,
name NVARCHAR(100)
)
INSERT INTO #temp(ID,name)
VALUES(1,'Akash'),
(1,'B'),
(1,'C'),
(2,'AA'),
(2,'BB')
We want to get the names as comma separated grouped by ID, so for ID 1 we will have 'Akash, B, C'. Below query will create a comma separated list grouped by ID.
select distinct u1.ID,
Replace((select distinct u2.name + ',' as 'data()'
from #temp u2
where u2.ID = u1.ID
FOR XML PATH('')) + '$', ',$', '') as Names
from #temp u1
order by u1.ID
No comments:
Post a Comment