table A
CLASS     NAME
    1        A
    1        B
    1        C
    2        D
    2        E
    3        F
我想查询出这样的结果
CLASS     NAME
    1    A+B+C
    2      D+E
    3        F
请问什么写sql啊?谢谢路过大虾帮忙~

解决方案 »

  1.   

    回复人: natumi(夏実) ( ) 信誉:100
    没办法,有这个业务需求~唉~努力想想,帮帮忙~
      

  2.   

    create function f_str(@id int)
    returns varchar(8000)
    as 
    begin
      declare @ret varchar(8000)
      set @ret =''
      select @ret =@ret+'+'+ name from tableA where class=@id
      set @ret=stuff(@ret,1,1,'')
      return @ret
    end
    gocreate table tableA(class int, name Varchar(10))
    insert tableA select 1,'A'
    union all select 1,'B'
    union all select 1,'C'
    union all select 2,'D'
    union all select 2,'E'
    union all select 3,'F'select distinct name=dbo.f_str(class)from tableA
    drop table tableA
    drop function f_strname                                                                  
    -------------------------------------------------------------------------------------
    A+B+C
    D+E
    F(3 row(s) affected)