使用PIVOT时出错

时间:2017-07-07 09:25:45

标签: sql database pivot-table rdbms

SELECT *
FROM ( SELECT YEAR(CreateDate) [Year],
              MONTH(CreateDate) [Month],
              DATENAME(MONTH, CreateDate) [Month Name],
              COUNT(id) [Transaction Count],
              CASE
                  WHEN PaymentStatus =  1 THEN 'Pending'
                  WHEN PaymentStatus =  2 THEN 'Commited'
                  WHEN PaymentStatus =  3 THEN 'Confirmed'
                  WHEN PaymentStatus =  4 THEN 'Canceled'
                  WHEN PaymentStatus =  5 THEN 'Failed'
                  ELSE ''
              END AS PaymentStatus,
              COUNT(id) as counts 
       FROM [dbo].[BankPaymentRequest]
       GROUP BY YEAR(CreateDate),
                MONTH(CreateDate),
                DATENAME(MONTH, CreateDate),
                PaymentStatus
     ) AS asd
PIVOT( SUM(counts)
       FOR PaymentStatus IN ( [Pending],
                              [Commited],
                              [Confirmed],
                              [Canceled],
                              [Failed]
                            )
     ) AS MNamePivot

Having some multiple rows for same months, only 12 months should display.

0 个答案:

没有答案