Transform Rows to columns with out using PIVOT

  • Hi Team,

    I have the below requirement my table has the below data

    TABLENAME - SOURCE

    EMPID ENAME DEPARMENT

    1 XXX IT

    2 YYY SALES

    3 ZZZ MARKETING

    4 OOO COMPUTERS

    EXPECTED OUTPUT

    EMPID IT SALES MARKETING COMPUTERS

    1 XXX NULL NULL NULL

    2 NULL YYY NULL NULL

    3 NULL NULL ZZZ NULL

    4 NULL NULL NULL OOO

  • sqlfriends@sql.com (1/28/2016)


    Hi Team,

    I have the below requirement my table has the below data

    TABLENAME - SOURCE

    EMPID ENAME DEPARMENT

    1 XXX IT

    2 YYY SALES

    3 ZZZ MARKETING

    4 OOO COMPUTERS

    EXPECTED OUTPUT

    EMPID IT SALES MARKETING COMPUTERS

    1 XXX NULL NULL NULL

    2 NULL YYY NULL NULL

    3 NULL NULL ZZZ NULL

    4 NULL NULL NULL OOO

    Just use a CASE statement for each column without aggregates.

    SELECT EMPID, CASE WHEN DEPARTMENT = 'IT' THEN ENAME END, ...

    FROM TABLE;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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