SQL Pivot Table multiple Columns

ojmp2001 ojmp2001 121 Reputation points
2020-09-23T17:47:03.137+00:00

How do I pivot multiple columns
CREATE TABLE #tblTest
(
AREA VARCHAR(10)
,LEVEL1 VARCHAR(3)
,LEVEL2 VARCHAR(3)
,Amount Decimal(5)

)
INSERT #tblTest VALUES ('HR', 'ABC', '', 100)
INSERT #tblTest VALUES ('COMP', 'BCD', '', 200)
INSERT #tblTest VALUES ('TECH','CDF', 'A1',5)
INSERT #tblTest VALUES ('TECH','CDF', 'A2',10)
INSERT #tblTest VALUES ('TECH','CDF', 'A3',15)
INSERT #tblTest VALUES ('OPR','DEF', '',90)

How I want it to look like.

27503-capture.jpg

----------

Heading

Developer technologies Transact-SQL
SQL Server Other
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2020-09-23T19:31:01.803+00:00

    Try something like this:

    drop table if exists #tblTest  
      
    CREATE TABLE #tblTest  
    (  
        AREA VARCHAR(10),  
        LEVEL1 VARCHAR(3),  
        LEVEL2 VARCHAR(3),  
        Amount Decimal(5)  
    )  
      
    INSERT #tblTest VALUES ('HR', 'ABC', '', 100)  
    INSERT #tblTest VALUES ('COMP', 'BCD', '', 200)  
    INSERT #tblTest VALUES ('TECH','CDF', 'A1',5)  
    INSERT #tblTest VALUES ('TECH','CDF', 'A2',10)  
    INSERT #tblTest VALUES ('TECH','CDF', 'A3',15)  
    INSERT #tblTest VALUES ('OPR','DEF', '',90)  
      
    select * from #tblTest  
      
    ---  
      
    declare @columns as nvarchar(max)  
      
    select @columns = STRING_AGG(QUOTENAME(case when LEVEL2 is null or LEVEL2 = '' then LEVEL1 else LEVEL2 end), ', ')  
    from #tblTest  
      
    declare @q as nvarchar(max) =   
    N'select [AREA] as [ ], ' + @columns + ' from ( ' +  
    ' select [AREA], case when LEVEL2 is null or LEVEL2 = '''' then LEVEL1 else LEVEL2 end as V, Amount  
     from #tblTest   
    ) t  
    pivot  
    (  
        SUM(Amount) for V in (' + @columns + ')  
    ) p  
    '  
      
    --PRINT @q  
      
    EXEC ( @q )  
    

    The order of rows and columns was not specified. Empty cells are shown as ‘NULL’.

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. ojmp2001 ojmp2001 121 Reputation points
    2020-09-23T19:09:08.863+00:00

    yes this is a dynamic query.
    The values in LEvel2 for CDF is what used as columns

    0 comments No comments

  2. EchoLiu-MSFT 14,621 Reputation points
    2020-09-24T09:05:45.703+00:00

    Hi @ojmp2001 ojmp2001

    It can also be achieved with ordinary SQL, please refer to:

        CREATE TABLE #tblTest  
        (  
        AREA VARCHAR(10)  
        ,LEVEL1 VARCHAR(3)  
        ,LEVEL2 VARCHAR(3)  
        ,Amount Decimal(5)  
          
        )  
        INSERT #tblTest VALUES ('HR', 'ABC', '', 100)  
        INSERT #tblTest VALUES ('COMP', 'BCD', '', 200)  
        INSERT #tblTest VALUES ('TECH','CDF', 'A1',5)  
        INSERT #tblTest VALUES ('TECH','CDF', 'A2',10)  
        INSERT #tblTest VALUES ('TECH','CDF', 'A3',15)  
        INSERT #tblTest VALUES ('OPR','DEF', '',90)  
          
          
        with cte  
        as(select AREA,case when LEVEL2='' then LEVEL1 else LEVEL2 end rn,Amount from #tblTest)  
        ,cte2 as  
        (select * from (select AREA,rn,Amount from cte) as t    
        pivot (max(Amount) for rn in (ABC,BCD,A1,A2,A3,DEF)) as p)  
        ,cte3 as  
        (select *,case when AREA='HR' then 1  
                      when AREA='COMP'then 2  
           when AREA='TECH'then 3  
           when AREA='OPR'then 4  
           end rn from cte2)  
          
        select AREA,ABC,BCD,A1,A2,A3,DEF   
        from cte3   
        order by rn  
          
        drop table #tblTest  
    

    27936-image.png

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Best Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.