• This is using the PIVOT operator. Case statements can also work and sometimes perform better.

    SELECT *

    FROM (SELECT RowNum = Row_number()

    OVER(

    PARTITION BY codes.CodeName

    ORDER BY CreatedDate DESC)

    ,codes.CodeName

    ,data.Quote

    FROM @QuoteData data

    JOIN @CodeTypes codes

    ON data.CodeID = codes.CodeID) AS quotes

    PIVOT ( Max(Quote)

    FOR CodeName IN ([AutoLoan],

    [CashAdvance],

    [CreditRefi],

    [HomeLoan],

    [Promotion])

    )AS pvt

    WHERE pvt.RowNum = 1