Need to get the multiple row in single
Hi Friends,Here is me table:
id Name
----- ---------
1 Raj
1 Kumar
2 A
2 B
i need result as 1 | Raj,kumar
2 | A,B
I have seen something called STUFF, but dont know how to use it. Any idea???
Thanks in advance.
WITH tempTable AS
(
SELECT ID FROM Tablename
GROUP BY ID
)
SELECT t.ID, Name= STUFF((SELECT ',' + Name FROM Tablename tn WHERE tn.ID = t.ID FOR XML PATH('')), 1, 1, '')
FROM tempTable t;
SELECT Id,
Stuff((SELECT ', ' + code
FROM tblmta t2
WHERE t2.Id = t1.Id
FOR XML PATH('')), 1, 2, '') [Name]
FROM tblmta t1 GROUP BY ID