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 zc02
1 zc03
2 xx01
2 xx02
2 xx03
要求结果
deptid bblistid
1 zc01,zc02,zc03
2 xx01,xx02,xx03
bblistid=stuff((select ','+bblistid from tb where deptid=a.deptid for xml path('')),1,1,'')
from tb a group by deptid
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
参照其它方法
http://topic.csdn.net/u/20080612/22/c850499f-bce3-4877-82d5-af2357857872.html