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.
| Author |
Topic |
|
musclebreast
Yak Posting Veteran
77 Posts |
Posted - 2012-09-13 : 19:20:48
|
Hello,I' got this SQL:select * from data A2 WHEREA2.ID ='536031'ANDA2.AttrID = '6' That code gives me the following result:ID.........Version....AttrID.......group536031......1.........6..............test536031......2.........6..............test5536031......3.........6..............test5536031......3.........6..............test6What I try to do. I want to get all groups of the last version. The Groups schould be in one column...the the result must be the follwoing:ID.........group536031...test5, test6At first you can throught the table. With the function mx you can get the last number of the version and then I need to filter the data again with the right attrid...Is my way correct or is there a better one?Kind regdards,Lara |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-13 : 22:02:34
|
| [code];With CTEAS(select * from data A2 WHEREA2.ID ='536031'ANDA2.AttrID = '6' )SELECT ID,STUFF((SELECT ',' + [group] FROM CTE WHERE ID = c.ID ORDER BY [Version] FOR XML PATH('')),1,1,'')FROM (SELECT DISTINCT ID FROM CTE)c[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|