Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help to writing sql query

Author  Topic 

anujpratap84
Starting Member

45 Posts

Posted - 2011-12-04 : 23:06:42
Hi All,

I have a table with two columns.
Name Role
A 1
B 2
C 3
D 4
E 5
F 6
A 4
C 5

I want the output Like
Name Role
A 1,4
B 2
C 3,5
D 4
E 5
F 6

Please Help..

Anuj Pratap Singh

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-04 : 23:36:10
refer to http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-04 : 23:57:24
[code]
SELECT Name,
STUFF((SELECT ',' + CAST(Role AS varchar(5)) FROM table WHERE Name = t.Name ORDER BY Role FOR XML PATH('')),1,1,'') AS [Roles]
FROM (SELECT DISTINCT Name FROM table)t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -