Convert column into row

  • Hi all,

    I have table like this:

    ID colour

    1 red

    1 blue

    1 orange

    2 red

    2 blue

    2 orange

    3 red

    3 blue

    I want like this

    ID colur

    1 red blue orange

    2 red blue orange

    3 red blue

  • There are a couple of ways you can do this. Here is one example.

    Declare @Table Table (id int, color varchar(10))

    Insert Into @Table

    Select 1, 'red' UNION ALL

    Select 1, 'blue' UNION ALL

    Select 1, 'orange' UNION ALL

    Select 2, 'red' UNION ALL

    Select 2, 'blue' UNION ALL

    Select 2, 'orange' UNION ALL

    Select 3, 'red' UNION ALL

    Select 3, 'blue'

    SELECT

    t1.ID,

    List = substring((SELECT ( ', ' + color )

    FROM @Table t2

    WHERE t1.ID = t2.ID

    ORDER BY

    ID

    FOR XML PATH( '' )

    ), 3, 1000 )FROM @Table t1

    GROUP BY ID

  • Ken Simmons (4/26/2009)


    There are a couple of ways you can do this. Here is one example.

    Declare @Table Table (id int, color varchar(10))

    Insert Into @Table

    Select 1, 'red' UNION ALL

    Select 1, 'blue' UNION ALL

    Select 1, 'orange' UNION ALL

    Select 2, 'red' UNION ALL

    Select 2, 'blue' UNION ALL

    Select 2, 'orange' UNION ALL

    Select 3, 'red' UNION ALL

    Select 3, 'blue'

    SELECT

    t1.ID,

    List = substring((SELECT ( ', ' + color )

    FROM @Table t2

    WHERE t1.ID = t2.ID

    ORDER BY

    ID

    FOR XML PATH( '' )

    ), 3, 1000 )FROM @Table t1

    GROUP BY ID

    Hi, I tried looking up the MSDN forum for XML PATH construct; I am still struggling to understand what does the (substring [select for xml path] 3, 1000) do in the code above. Kindly help...

    Saurabh Dwivedy
    ___________________________________________________________

    My Blog: http://tinyurl.com/dwivedys

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537

    Be Happy!
  • Easiest answer: Try and you'll see the difference...

    Or, to explain it without trying:

    The substring function removes the first two characters, which are a comma and a blank as per the "SELECT ( ', ' + color )" statement.

    Otherwise the result set would look like ", red, blue, orange" instead of "red, blue, orange".

    So, if you decide to separate the values by a blank character instead of comma + blank you'd need to change the SUBSTRING function to start a Position 2 instead of 3.



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • Thanks Lutz. I understand the substring part. I was trying to make sense of the 1000 part. Anyway I have got it now. Thanks for taking the time to explain.

    Saurabh Dwivedy
    ___________________________________________________________

    My Blog: http://tinyurl.com/dwivedys

    For better, quicker answers, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537

    Be Happy!
  • The 1000 part is just an impossibly long number of characters to make up for the fact that we don't know exactly how long the string is. You start at position 3 and get the next 1000 characters, except that 'red' has only 3 and 'orange' has only 6 characters. The SUBSTRING function stops at the end of the string.

    Todd Fifield

  • I have a similar problem but I don't know what the values are. I want to build a row that has a list of ID's

    Here is a sample dataset

    ACCOUNT_ID IND_ID

    123456 0234569

    123456 0321543

    123456 0345737

    123456 0785785

    123456 0934625

    When I select the IND_ID where ACCOUNT_ID = 123456 I want the results to be on a single line

    0234569, 0321543, 0345737, 0785785, 0934625

    All suggestions are gratefully received.

    thanks

  • Marcus why don't you try the supplied solution and just add a where clause?

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • 🙂 Because it's still early in the day and I haven't drunk enough coffee. 🙂

    thanks it worked

Viewing 9 posts - 1 through 8 (of 8 total)

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