网友问:
CODE NAME
1 A
1 B
如何得到下面的值,该如何写SQL
CODE NAME
1 A+B
1 A+B
俺的回答:
declare @t table(code varchar(50),name varchar(50))
insert into @t(code,name) select '1','A' union select '1','B'
select a.code,g.name_sum as name
from @t a,(select code,STRING_AGG (name,'+') as name_sum from @t group by code ) g
where a.code=g.code
其实就是个 字符串聚合 ,有很多写法。