select a,stuff((select ';'+cast(b as varchar) from tb where t.a=a),1,1,'')b from tb as t group by a
额。。2000只能写函数处理了, select @i=isnull(@i+',','')+cast(b as varchar) from tb where a=@a select @i
--创建表 if OBJECT_ID('Test') is not null drop table Test create table Test(A NVARCHAR(10), B NVARCHAR(10)) GO INSERT INTO Test SELECT 'a','1' UNION ALL SELECT 'a','2' UNION ALL SELECT 'a','3' UNION ALL SELECT 'b','1' UNION ALL SELECT 'b','4' UNION ALL SELECT 'c','5' --所要的结果 select A, B=stuff((select ';'+ltrim(B) from Test t WHERE t.A=tt.A for xml path('')), 1, 1, '') from Test tt group by tt.A DROP TABLE Test
from tb as t
group by a
select @i=isnull(@i+',','')+cast(b as varchar) from tb where a=@a
select @i
if OBJECT_ID('Test') is not null drop table Test
create table Test(A NVARCHAR(10), B NVARCHAR(10))
GO
INSERT INTO Test
SELECT 'a','1' UNION ALL
SELECT 'a','2' UNION ALL
SELECT 'a','3' UNION ALL
SELECT 'b','1' UNION ALL
SELECT 'b','4' UNION ALL
SELECT 'c','5'
--所要的结果
select A,
B=stuff((select ';'+ltrim(B) from Test t WHERE t.A=tt.A for xml path('')), 1, 1, '')
from Test tt group by tt.A
DROP TABLE Test