一.工作场景
背景:工作中经常会遇到一对多情况,将某列转成一对一可用到for xml path
举例:
create table #b (name nvarchar(10),title nvarchar(50))
insert into #b
values('周','1'),('周','2'),('周','3'),('吴','4'),('郑','5'),('郑','6'),('王','7')
select * from #b
将name-title 转成一对一:
select name,(
select title+';' from #b b where a.name = b.name for xml path('')
) as name
from #b a
group by name
-----------------------------------