transpose rows to columns in sql

前端 未结 3 1324
广开言路
广开言路 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: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.

提交回复
热议问题