SELECT * FROM (
SELECT YEAR(CreateDate) [Year], MONTH(CreateDate) [Month], DATENAME(MONTH,CreateDate) [Month Name],COUNT(id) [Total],
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
從SQL得到月明智的數據我想從這個查詢與各自[待定]年月明智的數據,[COMMITED],[確定],[取消],[失敗]事務計數。基本上我想要明確月交易計數待定,已委託,確認,取消和失敗。如何使用PIVOT
我正在使用Sql Server 2016 現在我得到確切的數據,但幾個月列出兩次或三次。
我要像下面的格式
Year Month Total Pending Commited Confirmed Canceled Failed
2016 Jan 34 1 4 63 840 157
2016 Feb 34 8 4 62 8 15
2016 Mar 65 1 4 63 840 157
2016 Dec 56 8 4 62 8 15
2017 Jan 78 1 4 63 840 157
2017 Feb 89 8 4 62 8 15
2017 Mar 67 1 4 63 840 157
2017 Dec 8 4 62 8 15 345
請幫我這樣的查詢或任何其他查詢數據。
首先,您需要確定您正在使用哪個DBMS。 mysql <> sql服務器。那麼你需要提供一些細節,以便其他人可以提供幫助。這裏是一個開始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –
如果你確定關於sql server 2008,那麼刪除mysql –