[英]Count datetime entries for each month from datetime column in SQL Server
I have a table with timestamps and I want to count the total number of timestamps for each month. 我有一个带有时间戳的表,我想计算每个月的时间戳总数。 I have the following script returning the results.
我有以下脚本返回结果。 Is there a simpler way to do this?
有没有更简单的方法可以做到这一点?
select
substring(convert(varchar(12), dt_create, 112), 0, 7) as month,
count(substring(convert(varchar(12), dt_create, 112), 0, 7)) as signups
from
table_name
group by
substring(convert(varchar(12), dt_create, 112), 0, 7)
order by
1
Output 输出量
month signups
----------------
201705 5959
201706 9782
201707 13663
201708 7385
Characters values are slower, you may see an increase in performance by trying this. 字符值较慢,尝试此操作可能会提高性能。
SELECT Year = YEAR( dt_create ),
Month = MONTH( dt_create ),
signups = COUNT( dt_create )
FROM table_name
GROUP BY YEAR( dt_create ),
MONTH( dt_create )
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.