将多行转换为一行,用某个符号隔开的SQL:
虽然网上很多方法,但是不工整且不规范,这里给大家分享一种简介规范的写法:
Select * Into #temp From ( select ‘A‘ id,‘aa‘ name union all select ‘A‘ id,‘bb‘ name union all select ‘A‘ id,‘cc‘ name union all select ‘B‘ id,‘aa‘ name )a Select * From #temp --查询 Select id, name=stuff((select ‘,‘+name from #temp where id=t.id for xml path(‘‘)),1,1,‘‘) From #temp t Group by id Drop Table #temp
效果: