transpose rows to columns in sql

前端 未结 3 1325
广开言路
广开言路 2021-01-23 10:32

I have problem in getting the desired output with the SQL query.

My sql data is as follows:

TOTAL   Charge      PAYMNET      A         B        C                 


        
相关标签:
3条回答
  • 2021-01-23 11:09
    Select Month(Mdate)md,'A' AS Col,sum(A) as a from Product group by Month(MDate)
    union all
    Select Month(Mdate)md,'B',sum(b) as a from Product group by Month(MDate)
    union all
    Select Month(Mdate)md,'C',sum(c) as a from Product group by Month(MDate)
    union all
    Select Month(Mdate)md,'D',Count(A) as a from Product group by Month(MDate)
    

    Try Pivot with the above query you may to get required result....

    0 讨论(0)
  • 2021-01-23 11:11

    I have tried this code. Please check and let me know if it works

    I know that it doesnt look so good. Also not sure how it will be performance wise.

    --Can have more columns like A,B,...
    DECLARE @tbl TABLE
    (
    TOTAL INT,
    CHARGE FLOAT,
    PAYMENT FLOAT,
    MONTHYEAR VARCHAR(50)
    )
    
    
    --Test data
    INSERT INTO @tbl SELECT 661, 157832.24, 82967.80, 'Oct2013'
    INSERT INTO @tbl SELECT 612,     95030.52,    17824.28, 'Nov2013'
    INSERT INTO @tbl SELECT 584     ,90256.35,    16732.91, 'Dec2013'
    
    --Can be a physical table
    CREATE TABLE #FinalTbl 
    (
    DATA VARCHAR(100)
    )
    
    --inserted hardcode records in data column. To add it dynamically you would need to loop through information_schema.columns
    --SELECT *
    --FROM information_schema.columns
    --WHERE table_name = 'tbl_name'
    INSERT INTO #FinalTbl
    VALUES ('TOTAL')
    
    INSERT INTO #FinalTbl
    VALUES ('CHARGE')
    
    INSERT INTO #FinalTbl
    VALUES ('PAYMENT')
    
    DECLARE @StartCount INT, @TotalCount INT, @Query VARCHAR(5000), @TOTAL INT,@CHARGE FLOAT,@PAYMENT FLOAT,@MONTHYEAR VARCHAR(50)
    
    SELECT @TotalCount = COUNT(*) FROM @tbl;
    SET @StartCount = 1;
    
    WHILE(@StartCount <= @TotalCount)
    BEGIN
        SELECT @TOTAL = TOTAL, 
        @CHARGE = CHARGE,
        @PAYMENT = PAYMENT,
        @MONTHYEAR = MONTHYEAR  
        FROM
        (SELECT ROW_NUMBER() over(ORDER BY MONTHYEAR) AS ROWNUM, * FROM @tbl) as tbl
        WHERE ROWNUM = @StartCount
    
        SELECT @Query = 'ALTER TABLE #FinalTbl ADD ' + @MONTHYEAR + ' VARCHAR(1000)'
        EXEC (@Query)
    
        SELECT @Query = 'UPDATE #FinalTbl SET ' + @MONTHYEAR + ' = ''' + CONVERT(VARCHAR(50), @TOTAL) + ''' WHERE DATA = ''TOTAL'''
        EXEC (@Query)
    
        SELECT @Query = 'UPDATE #FinalTbl SET ' + @MONTHYEAR + ' = ''' + CONVERT(VARCHAR(50), @CHARGE) + ''' WHERE DATA = ''CHARGE'''
        EXEC (@Query)
    
        SELECT @Query = 'UPDATE #FinalTbl SET ' + @MONTHYEAR + ' = ''' + CONVERT(VARCHAR(50), @PAYMENT) + ''' WHERE DATA = ''PAYMENT'''
        EXEC (@Query)
    
        SELECT @StartCount = @StartCount + 1
    END
    
    SELECT * FROM #FinalTbl
    
    DROP TABLE #FinalTbl
    

    Hope this helps

    0 讨论(0)
  • 2021-01-23 11:16

    I would imagine the reason you are only getting 3 or 4 months is because you don't have data for the missing months? If you want to display columns for missing months you will need to either:

    1. Create a Table datatype with all the months you want to display and left join the remainder of the tables to it in your query. You could then use the PIVOT function as normal.

    2. If you know how many columns up front i.e. one for each month in a particular year and it won't change, you can simply use CASE Statements (one for each month) to transpose the data without the PIVOT operator.

    I can provide examples if needed.

    0 讨论(0)
提交回复
热议问题