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
 specific concatenate column values in a row

Author  Topic 

chopstix21
Starting Member

1 Post

Posted - 2012-05-23 : 09:46:58
i need to concatenate x values which are grouped by id, ordering them by y column from subquery.
Subquery looks like this
id x y
1 1 1
1 2 3
1 3 2
2 1 null
2 2 1
3 1 1
3 2 2
4 1 null
4 2 null
final result should look like
id result
1 1,3,2
2 2
3 1,2
4 null

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-05-23 : 09:55:22
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 - 2012-05-23 : 14:44:37
[code]
SELECT id,
STUFF((SELECT ',' + CAST(x AS varchar(10)) FROM table
WHERE id = t.id
AND y IS NOT NULL
ORDER BY y
FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT id FROM table)t
[/code]

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

Go to Top of Page
   

- Advertisement -