Optimal way to concatenate/aggregate strings

前端 未结 7 1236
[愿得一人]
[愿得一人] 2020-11-22 11:17

I\'m finding a way to aggregate strings from different rows into a single row. I\'m looking to do this in many different places, so having a function to facilitate this woul

相关标签:
7条回答
  • 2020-11-22 12:14

    Are methods using FOR XML PATH like below really that slow? Itzik Ben-Gan writes that this method has good performance in his T-SQL Querying book (Mr. Ben-Gan is a trustworthy source, in my view).

    create table #t (id int, name varchar(20))
    
    insert into #t
    values (1, 'Matt'), (1, 'Rocks'), (2, 'Stylus')
    
    select  id
            ,Names = stuff((select ', ' + name as [text()]
            from #t xt
            where xt.id = t.id
            for xml path('')), 1, 2, '')
    from #t t
    group by id
    
    0 讨论(0)
提交回复
热议问题