• rhythmk (5/13/2014)


    With the help of Eirikur Eiriksson's code.You acn try this.

    ;WITH test

    AS

    (SELECT POLICY_DETAILS_ID,HISTORY_ID,CODE_ID,

    ROW_NUMBER() OVER(PARTITION BY POLICY_DETAILS_ID,HISTORY_ID ORDER BY code_id) AS rn

    FROM @POLHIST)

    SELECT POLICY_DETAILS_ID,HISTORY_ID,

    MAX(CASE WHEN rn = 1 THEN code_id END) AS Code1,

    MAX(CASE WHEN rn = 2 THEN code_id END) as Code2,

    MAX(CASE WHEN rn = 3 THEN code_id END) as Code3,

    MAX(CASE WHEN rn = 4 THEN code_id END ) as Code4

    FROM test

    GROUP BY POLICY_DETAILS_ID,HISTORY_ID

    Is there an echo in here? 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/