A列     B列
a        1
a        2
a        3
b        1
b        4
c        5目标结果:
A列     B列
a      1;2;3
b      1;4
c      5

解决方案 »

  1.   

    select a,stuff((select ';'+cast(b as varchar) from tb where t.a=a),1,1,'')b
    from tb as t
    group by a
      

  2.   

    额。。2000只能写函数处理了,
    select @i=isnull(@i+',','')+cast(b as varchar) from tb where a=@a
    select @i
      

  3.   

    --创建表
    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