Columns to rows and rows to column conversion without pivot/unpivot

  • All I need is convert Column(A1/A2) into rows and rows(1 into Jan) into columns.Input:

    Here A1/A2 belongs to say A and they are calculated as A1/A2 for each month.

    MonthA1A2B1B2C1C2
    1120604080120120
    2505040206030
    3502540109030

    I need below o/p without using pivot and unpivot

    O/P:

    XJan(1 is denoting Jan)FebMar
    A120/60(calculation:A1/A2)40/80120/120
    B50/5040/2060/30
    C50/2540/1090/30

    I tried but my query is too long as I am using case and Union All three times each For A1 A2,B1 B2,C1 C2 etc.

    Can you pls help me out.
    Thanks in advance

  • First, you should learn to post readily consumable data instead of just plain text.  Please see the first link under "Helpful Links" in my signature line below.  People will answer much more quickly and do so with code instead of descriptions.

    Second, when you say things like you can't use Pivot or Unpivot, you need to explain why because the very same thing preventing their use my also prevent the use of other things, such a CROSS APPLY, which I've used below.  The code below also includes another method for creating readily consumable data.


    --===== Create the test table and populate.
         -- Most people that provide answers on this forum like to test
         -- their solution prior to posting.  With that in mind,
         -- please do this in the future instead of just posting text.
         -- See the first link in my signature line below for more.
         -- This is not the solution. We're just defining the test data here.
     SELECT *
       INTO #TestData
       FROM (VALUES
             (1,120,60,40,80,120,120)
            ,(2,50 ,50,40,20,60 ,30)
            ,(3,50 ,25,40,10,90 ,30)
            ) v ([Month],A1,A2,B1,B2,C1,C2)
    ;
    --===== Solve the problem using the ancient "Black Art" known as a CROSS TAB.
       WITH
    ctePreagg AS
    (
     SELECT v.X,td.[Month],Value = CONVERT(DECIMAL(9,3),Value)
       FROM #TestData td
      CROSS APPLY   (VALUES
                     ('A',(A1+0.0)/A2)
                    ,('B',(B1+0.0)/B2)
                    ,('C',(C1+0.0)/C2)
                    ) v (X,Value)
    )
     SELECT  X
            ,Jan = MAX(CASE WHEN [Month] = 1 THEN Value ELSE 0 END)
            ,Feb = MAX(CASE WHEN [Month] = 2 THEN Value ELSE 0 END)
            ,Mar = MAX(CASE WHEN [Month] = 3 THEN Value ELSE 0 END)
       FROM ctePreAgg
      GROUP BY X
      ORDER BY X
    ;

    --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)

  • Jeff, It's always fun to see the same question posted on 2 different forums... Looks like out two solutions are pretty similar... At least in terms of the "CROSS APPLY ( VALUES" unpivot & manual cross tab pivot...
    http://forums.sqlteam.com/t/rows-to-columns-and-columns-to-rows-without-pivot/11267

  • Jason A. Long - Saturday, August 26, 2017 8:18 PM

    Jeff, It's always fun to see the same question posted on 2 different forums... Looks like out two solutions are pretty similar... At least in terms of the "CROSS APPLY ( VALUES" unpivot & manual cross tab pivot...
    http://forums.sqlteam.com/t/rows-to-columns-and-columns-to-rows-without-pivot/11267

    Great minds do think alike.  😉

    I think it's a real shame that MS removed the CROSS TAB technique from BOL when they implemented that bloody crippled PIVOT function.  For anyone interested in reading about it, here's an ages-old article I wrote about it and a second article on how to easily build dynamic CROSS TABs. The first article also includes a concept known as "Pre-Aggregation" (I give Peter Larsson the credit for coining that phrase), which makes the CROSS TAB method literally twice as fast as the equivalent PIVOT but still has more flexibility when it comes to calculating row and column totals.

    http://www.sqlservercentral.com/articles/T-SQL/63681/
    http://www.sqlservercentral.com/articles/Crosstab/65048/

    I don't know who first came up with it but the first person I ever saw use CROSS APPLY as an UNPIVOT was Gianluca Sartori, the "Spaghetti DBA".  Here's a link to his blog...
    https://spaghettidba.com/

    --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)

  • Yea... It's damn near painful seeing people trying to do complex pivots using the PIVOT operator when it's so simple using the old cross tab method.
    As far as using the CROSS APPLY method for unpivoting data... IIRC the first time I saw it was in a Dwain Camps article.

  • arihantjain121189 - Saturday, August 26, 2017 10:19 AM

    All I need is convert Column(A1/A2) into rows and rows(1 into Jan) into columns.Input:

    Here A1/A2 belongs to say A and they are calculated as A1/A2 for each month.

    MonthA1A2B1B2C1C2
    1120604080120120
    2505040206030
    3502540109030

    I need below o/p without using pivot and unpivot

    O/P:

    XJan(1 is denoting Jan)FebMar
    A120/60(calculation:A1/A2)40/80120/120
    B50/5040/2060/30
    C50/2540/1090/30

    I tried but my query is too long as I am using case and Union All three times each For A1 A2,B1 B2,C1 C2 etc.

    Can you pls help me out.
    Thanks in advance

    SELECT Node,
      MAX(CASE WHEN month= 1 THEN Product ELSE NULL END) [Jan],
      MAX(CASE WHEN month= 2 THEN Product ELSE NULL END) [Feb],
      MAX(CASE WHEN month= 3 THEN Product ELSE NULL END) [Mar]
    FROM
    (
    select 'A' NODE, month, A1*A2 Product
    FROM table1
    UNION ALL
    select 'B' NODE, month, B1*B2 Product
    FROM table1
    UNION ALL
    select 'C' NODE, month, C1*C2 Product
    FROM table1
    )
    GROUP BY NODE;

  • anand08sharma - Monday, August 28, 2017 11:55 AM

    arihantjain121189 - Saturday, August 26, 2017 10:19 AM

    All I need is convert Column(A1/A2) into rows and rows(1 into Jan) into columns.Input:

    Here A1/A2 belongs to say A and they are calculated as A1/A2 for each month.

    MonthA1A2B1B2C1C2
    1120604080120120
    2505040206030
    3502540109030

    I need below o/p without using pivot and unpivot

    O/P:

    XJan(1 is denoting Jan)FebMar
    A120/60(calculation:A1/A2)40/80120/120
    B50/5040/2060/30
    C50/2540/1090/30

    I tried but my query is too long as I am using case and Union All three times each For A1 A2,B1 B2,C1 C2 etc.

    Can you pls help me out.
    Thanks in advance

    SELECT Node,
      MAX(CASE WHEN month= 1 THEN Product ELSE NULL END) [Jan],
      MAX(CASE WHEN month= 2 THEN Product ELSE NULL END) [Feb],
      MAX(CASE WHEN month= 3 THEN Product ELSE NULL END) [Mar]
    FROM
    (
    select 'A' NODE, month, A1*A2 Product
    FROM table1
    UNION ALL
    select 'B' NODE, month, B1*B2 Product
    FROM table1
    UNION ALL
    select 'C' NODE, month, C1*C2 Product
    FROM table1
    )
    GROUP BY NODE;

    That works but requires 3 scans of the table instead of just 1.

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

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