SQL Server : Group by string concatenation

后端 未结 2 1988
谎友^
谎友^ 2021-01-07 04:11

I have a question. I know that has been asked before. I looked through the related questions but I could not get my SQL script to work.

Here is my query :

         


        
2条回答
  •  伪装坚强ぢ
    2021-01-07 04:23

    http://sqlfiddle.com/#!3/d41d8/5441

    create table #Temp (PART_ID bigint, ID bigint, DESCRIPTION nvarchar(max))
    
    insert into #Temp
    select 10002, 1182505, 'Tagfahrlichtschaltung' union all
    select 80029, 1182505, 'Bluetooth' union all
    select 20004, 1212866, 'Kindersitzbefestigung' union all
    select 10045, 1212866, 'Lederlenkradrriegelung' union all
    select 11908, 1257946, 'Airbag' union all
    select 22346, 1257946, 'Automatic'
    
    select 
        T1.ID,
        stuff(
            (
                select ' ; ' + cast(T2.PART_ID as nvarchar(max)) + ' : ' + T2.DESCRIPTION
                from #TEmp as T2
                where T2.ID = T1.ID 
                for xml path(''), type
            ).value('data(.)', 'nvarchar(max)')
        , 1, 3, '') as LISTOFPARTS
    from #TEMP as T1 
    group by T1.ID
    order by T1.ID
    

提交回复
热议问题