Transpose

  • Hi,

    Here is what i have

    CId, SN

    1, ABC1

    1, AAA2

    1, BCV3

    2, XYZ1

    2, MMM1

    Here is what i need

    Id SNs

    1, ABC1,AAA2,BCV3

    2, XYZ1,MMM1

    How?

    Thanks

  • Something like this, I would guess.

    create table #testtab (

    CId int,

    SN char(4)

    );

    insert into #testtab

    select 1, 'ABC1' union all

    select 1, 'AAA2' union all

    select 1, 'BCV3' union all

    select 2, 'XYZ1' union all

    select 2, 'MMM1';

    select distinct

    CId,

    STUFF((select ',' + SN from #testtab tt2 where tt2.CId = tt1.CId for xml path('')),1,1,'')

    from

    #testtab tt1;

  • Thanks Lynn, you are the best 🙂

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply