Sql 如何使用SELECT语句从表列创建逗号分隔的列表
提前谢谢 我有一张表,上面有这样的值Sql 如何使用SELECT语句从表列创建逗号分隔的列表,sql,sql-server,Sql,Sql Server,提前谢谢 我有一张表,上面有这样的值 class_id Instructor_Name ——————————— 1 Joe 2 Joe 3 Joe 1 Judy 2 Judy 2 Kevin 3 Kevin 我希望使用SQL语句得到如下结果集: id name services (I want) ———————————– 1 Joe, Judy 2 Joe, Judy, KevinC 3 Joe, Kevin 我怎么能得到这个 谢谢, Brijesh对X
class_id Instructor_Name
———————————
1 Joe
2 Joe
3 Joe
1 Judy
2 Judy
2 Kevin
3 Kevin
我希望使用SQL语句得到如下结果集:
id name services (I want)
———————————–
1 Joe, Judy
2 Joe, Judy, KevinC
3 Joe, Kevin
我怎么能得到这个
谢谢,
Brijesh对XML路径使用
STUFF
和与相关子查询:
select
class_id,
stuff(
(select ', ' + Instructor_Name from your_table b
where a.class_id = b.class_id for xml path('')),
1, 2, ''
) names
from your_table a
group by
class_id;
您使用的是哪种DBMS?我这里使用的是MS SQL SeverCheck解决方案:非常感谢,GurV,工作非常出色。我试图找到之前发布的问题,但找不到正确的关键字。再次感谢。