如何统计一个值在每个月和每年的个数? SQL

How to count the number of a value in each month and year? SQL

如何统计每个月和每年的'cancel'值的个数?谢谢!

 res                   date
---------------------------------------   
cancel                 2022-05-02
cancel                 2022-04-22
cancel                 2022-04-08
cancel                 2022-03-01

MS SQL

预期输出:

 month&year          numcases
-------------------------------
05-2022                  300
04-2022                  543

等等……

您可以开始尝试这样的事情:

SELECT YEAR([date]), MONTH([date]), COUNT(*) AS [Number]
FROM [Table]
GROUP BY YEAR([date]), MONTH([date])

[日期] 列应该是日期时间。如果它是文本,你将不得不投射它:

SELECT YEAR(CAST([date] AS datetime)), ...