Answered by:
how to create the csv column

Question
-
i want to create the csv col for the colname based on the below specified data
table:
id colname colval
1 A 1
2 B 1
3 A 2
4 B 2
5 C 2
i want the below format as o/p
id colname colval
1 A,B 1
2 A,B,C 2
please help me how to do this .............
Sudhesh. G
http://gurucoders.blogspot.com
Answers
-
CREATE TABLE #mable(mid INT, token nvarchar(16))
INSERT INTO #mable VALUES (0, 'foo')
INSERT INTO #mable VALUES(0, 'goo')
INSERT INTO #mable VALUES(1, 'hoo')
INSERT INTO #mable VALUES(1, 'moo')SELECT m1.mid,
( SELECT m2.token + ','
FROM #mable m2
WHERE m2.mid = m1.mid
ORDER BY token
FOR XML PATH('') ) AS token
FROM #mable m1
GROUP BY m1.mid ;
Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/- Proposed as answer by Manoj Pandey (manub22)Microsoft employee Tuesday, February 22, 2011 1:48 PM
- Marked as answer by Sudhesh.G Tuesday, February 22, 2011 2:17 PM
All replies
-
CREATE TABLE #mable(mid INT, token nvarchar(16))
INSERT INTO #mable VALUES (0, 'foo')
INSERT INTO #mable VALUES(0, 'goo')
INSERT INTO #mable VALUES(1, 'hoo')
INSERT INTO #mable VALUES(1, 'moo')SELECT m1.mid,
( SELECT m2.token + ','
FROM #mable m2
WHERE m2.mid = m1.mid
ORDER BY token
FOR XML PATH('') ) AS token
FROM #mable m1
GROUP BY m1.mid ;
Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/- Proposed as answer by Manoj Pandey (manub22)Microsoft employee Tuesday, February 22, 2011 1:48 PM
- Marked as answer by Sudhesh.G Tuesday, February 22, 2011 2:17 PM
-