34,590
社区成员
发帖
与我相关
我的任务
分享
select col1,col2,
col3=stuff((select ','+col3 from tb where t.col1=col1 and t.col2=col2 for xml path('')) ,1,1,'')
from 表 t
group by col1,col2
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
create table [TB]([col1] varchar(1),[col2] varchar(1),[col3] varchar(1))
insert [TB]
select 'a','b','c' union all
select 'a','b','d'
select ','+col3 from tb for xml path('')
select
col1,
col2,
col3=stuff((select ','+col3 from tb where t.col1=col1 and t.col2=col2 for xml path('')) ,1,1,'')
from [TB] t
group by col1,col2
/*
col1 col2 col3
---- ---- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
a b c,d
(1 行受影响)
*/
drop table [TB]