Convert table with single column of data into multiple columns

  • Hi All,

    I am very new to this and hope I have selected the correct forum to post this.

    I am attempting to convert data from one table with one “element” column which contains multiple elements into a new table with multiple columns of these elements. I am trying to write a function/procedure so that this can be done.

    User_id | element | value

    1 | a | x1

    1 | b | x2

    1 | c | x3

    1 | d | x4

    To a table looking like:

    User_id | a | b | c | d

    1 | x1 | x2 | x3 | x4

    There are multiple user_ids.

    This will not be a one off case as I would like the table updated on a regular basis.

    Can anyone offer me some advise on how I can achieve this?

    Thanks, Mat

  • Cross Tab or PIVOT is what you are looking for...

    Here are few links that has few samples on how to do cross tab reports:

    http://www.sqlservercentral.com/articles/T-SQL/63681/[/url]

    http://www.sqlservercentral.com/articles/cross+tab/65048/[/url]

    --Ramesh


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

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