开发者

showing non aggregate column in a group by query in SQL

开发者 https://www.devze.com 2023-01-06 23:53 出处:网络
i have a table in SQL 2008 as IDItems 1A 1B 2C 3D 3B i would like to get the result as IDItems 1A,B 2C 3B,D I have used cursors but it has considerably slowed the process , can i achieve the

i have a table in SQL 2008 as

ID     Items
1      A
1      B
2      C
3      D
3      B

i would like to get the result as

ID    Items
1     A,B
2     C
3     B,D

I have used cursors but it has considerably slowed the process , can i achieve the above result using group开发者_开发百科 by query or through any other way.

Thanks and Regards

Kapil


You are looking for a way to concatenate the results. I don't think MSSQL has a function for that, but here's a very nice tutorial on how to create a method like that: http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/


I think

GROUP_CONCAT

is the function you are looking for. Something like

SELECT id,
       GROUP_CONCAT (DISTINCT Items ORDER BY Items SEPARATOR ',')
    FROM my_table
    GROUP BY id;
0

精彩评论

暂无评论...
验证码 换一张
取 消