deptid  bblistid  1       zc01
  1       zc02
  1       zc03
  2       xx01
  2       xx02
  2       xx03
要求结果
 deptid   bblistid
  1       zc01,zc02,zc03
  2       xx01,xx02,xx03

解决方案 »

  1.   

    select deptid,
           bblistid=stuff((select ','+bblistid from tb where deptid=a.deptid for xml path('')),1,1,'')
    from tb a group by deptid
      

  2.   

    Create function fn_merge(@deptid int)
    returns varchar(1000)
    as
    begin
    declare @str  varchar(1000)
    set @str=''
    select @str=@str+','+bblistid from 表 where deptid=@deptid
    set @str=stuff(@str,1,1,'')
    return @str
    endgo
    select deptid,dbo.fn_merge(deptid) as bblistid  from 表 group by deptid
      

  3.   

    SQL2000只能用函数,05用xml或CTE都可以实现
    参照其它方法
    http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html