已存在的表
A 3
A 5
A 6
B 1
B 2
B 3
我想要结果是
A 3,5,6
B 1,2,3
create function dbo.f_str(@col1 varchar(100))
returns varchar(100)
as
begin
declare @s varchar(100)
set @s =''
select @s=@s+', '+cast(列名2 as varchar(100))
from
where 列名1 =@col1
return (stuff(@s,1,1,''))
end
go
drop function dbo.f_str
select distinct 列名1,列名2=dbo.f_str(列名1) from dbo.test order by name