rows into columns

  • I'm tring to get results that has first name and last name in a single row rather than column, any help would be appreciated.

    SELECT distinct FIELD_ID,

    CASE WHEN FIELD_ID = 6 THEN FIELD_VALUE ELSE '' END AS [FirstName],

    CASE WHEN FIELD_ID = 7 THEN FIELD_VALUE ELSE '' END AS [LastName]

    From tblTemp

    Current Table:

    FIELD_IDFIELD_VALUEUSER_ID

    7Jones196

    6Tom196

    7West197

    6Dave197

    Desired Results:

    USER_IDFirst_NameLast_Name

    196TomJones

    197DaveWest

    thanks

  • mnicholas (10/24/2011)


    I'm tring to get results that has first name and last name in a single row rather than column, any help would be appreciated.

    SELECT distinct FIELD_ID,

    CASE WHEN FIELD_ID = 6 THEN FIELD_VALUE ELSE '' END AS [FirstName],

    CASE WHEN FIELD_ID = 7 THEN FIELD_VALUE ELSE '' END AS [LastName]

    From tblTemp

    Current Table:

    FIELD_IDFIELD_VALUEUSER_ID

    7Jones196

    6Tom196

    7West197

    6Dave197

    Desired Results:

    USER_IDFirst_NameLast_Name

    196TomJones

    197DaveWest

    thanks

    You were almost there...

    SELECT [User_ID],

    MAX(CASE WHEN FIELD_ID = 6 THEN FIELD_VALUE ELSE '' END) AS [FirstName],

    MAX(CASE WHEN FIELD_ID = 7 THEN FIELD_VALUE ELSE '' END) AS [LastName]

    FROM tblTemp

    GROUP BY [User_ID]

    ;

    --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 2 posts - 1 through 1 (of 1 total)

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