Convert Rows into Columns

  • Hello All,

    I'm trying to convert rows in table to columns as per the example i.e create one long string of data for every occurrence of ID column. Could anyone please help in achieving it in best way.

    /** Build up a Table to work with. **/
    DECLARE @T TABLE
    (
    ID INT NOT NULL
    , hProp INT NOT NULL
    , iDayOfMonth INT NOT NULL
    , dblTargetPercent DECIMAL(6,4) NOT NULL
    )

    INSERT INTO @T
    (ID, hProp, iDayOfMonth, dblTargetPercent)
    VALUES (117,10,5,0.1400)
    , (117, 10, 10, 0.0500)
    , (117, 10, 15, 0.0100)
    , (117, 10, 20, 0.0100)
    , (118, 20, 20, 0.0100)
    , (118, 30, 10, 0.0200)

    SELECT * FROM @T;

    OutputData

    Thanks and Regards,

    Loki

  • This is a duplicate post.  Please use the following link to get to the duplicate and the answers.  No posts on this link, please.  It will only dilute the information about how to accomplish this task.  Thanks, folks.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

The topic ‘Convert Rows into Columns’ is closed to new replies.