Switch between Columns and rows in a given table

  • Hi guys,

    I am looking for a query that can switch my rows and columns in the table below :

    The final result will look like that :

    Many Thanks

  • First unpivot, then pivot:


    IF OBJECT_ID('tempdb..#test') IS NOT NULL
    DROP TABLE #test;

    CREATE TABLE #test(
       populationtypedescription varchar(10)
      ,totalload                 int
      ,totalfirstload            int
    );
    INSERT INTO #test VALUES ('pop2',20,17);
    INSERT INTO #test VALUES ('pop3',3,2);
    INSERT INTO #test VALUES ('pop4',4,4);

    SELECT *
    FROM #test
    UNPIVOT (value FOR col IN (totalload, totalfirstload)) AS u
    PIVOT (MIN(value) FOR populationtypedescription IN ([pop2],[pop3],[pop4])) AS p

    -- Gianluca Sartori

  • spaghettidba - Thursday, July 6, 2017 3:23 AM

    OK , so i have another issue

    I tried to add another field which is defined as DATE type

    and the vdate field creates a new conflict

    Any suggestions how to convert it to an int?

    Thanks again

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

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