PIVOTing only selected rows

  • Hi,

    I have the following table:

    AA | BB

    =======

    1       a

    2      b

    1      c

    2     d

    1     e

    2     f

    --------

    I would like to obtain the following result table:

    AA |  BB  | 2

    =========

    1        a       b

    1        c       d

    1        e       f

    --------------

    I tried PIVOT but I think it doesn't work because not all rows are pivoted. Do I need to do self join? Is there any more efficient way than self join? Could you please post any examples?

    Thanks for help.

  • I created a CTE table for AA=1 and used a join with a table containing only the rows with AA =2. That is working.

  • This isn't really a PIVOT. You've selected where AA=1, and then also selected where AA=2 and renamed the column to "2", and somehow magically need to get them side by side. The problem I see is there isn't anything in your data that matches the '1' rows to the '2' rows, to give your result. How do you decide that 'b' should be on the same row as 'a' (and so on)?

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

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