UNPIVOTING Columns

  • I have a table that looks like this:

    COL1COL2COL3COL4COL5COL6
    12AABBCCDD
    13AABBCCDD
    21AABBCCDD
    22AABBCCDD

    I am trying to un-pivot based off COL1 and COL2 for COL3,COL4,COL5,COL6, but I am doing something very wrong.


    SELECT COL1, COL2, NEW_COLUMN
    FROM TABLE
    UNPIVOT
    (
    VALUE FOR COL IN (COL3,COL4,COL5,COL6) AS NEW_COLUMN
    )

    Can someone please help. I know I can do it with unions, but isn't using the pivot function the best method?

  • This should work

    SELECT COL1, COL2, VALUE AS [NEW_COLUMN]
    FROM TABLE AS p
    UNPIVOT (
      VALUE FOR COL IN ([COL3],[COL4],[COL5],[COL6])
    ) AS up
  • DesNorton - Friday, May 5, 2017 3:52 PM

    This should work

    SELECT COL1, COL2, VALUE AS [NEW_COLUMN]
    FROM TABLE AS p
    UNPIVOT (
      VALUE FOR COL IN ([COL3],[COL4],[COL5],[COL6])
    ) AS up

    one small update to Des' script so that you don't lose data in the unpivot process:
    SELECT COL1, COL2,
    COL as PIVOTCOL,  --< here's my update
    VALUE AS
    [NEW_COLUMN]
    FROM TABLE AS p
    UNPIVOT (
    VALUE FOR COL IN ([COL3],[COL4],[COL5],[COL6])
    ) AS up

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • I am getting the error 

    The type of column "COL4" conflicts with the type of other columns specified in the UNPIVOT list.

    When running :


    SELECT COL1, COL2,
    COL as PIVOTCOL, --< here's my update
    VALUE AS [NEW_COLUMN]
    FROM TABLE AS p
    UNPIVOT (
    VALUE FOR COL IN ([COL3],[COL4])
    ) AS up

  • Nevermind, datatypes need to be the same! Any way I can do this without making a temp table?

  • Maybe something like this would work.

    SELECT COL1, COL2, NEW_COLUMN
    FROM TABLE
    CROSS APPLY (VALUES('COL3', CAST( COL3 AS varchar(100))),
           ('COL4', CAST( COL4 AS varchar(100))),
           ('COL5', CAST( COL5 AS varchar(100))),
           ('COL6', CAST( COL6 AS varchar(100))))up(COL, NEW_COLUMN);

    --Or removing the caption to identify rows.
    SELECT COL1, COL2, NEW_COLUMN
    FROM TABLE
    CROSS APPLY (VALUES(CAST( COL3 AS varchar(100))),
           (CAST( COL4 AS varchar(100))),
           (CAST( COL5 AS varchar(100))),
           (CAST( COL6 AS varchar(100))))up(NEW_COLUMN);

    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
  • Thank you so much for the help. (ALL OF YOU!!!)

    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Viewing 7 posts - 1 through 6 (of 6 total)

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