我有一张带有时间戳的表格,我想计算每个月的时间戳总数。我有以下脚本返回结果。有更简单的方法吗?
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
输出
month signups
----------------
201705 5959
201706 9782
201707 13663
201708 7385
答案 0 :(得分:2)
字符值较慢,尝试此操作时可能会看到性能提升。
SELECT Year = YEAR( dt_create ),
Month = MONTH( dt_create ),
signups = COUNT( dt_create )
FROM table_name
GROUP BY YEAR( dt_create ),
MONTH( dt_create )
;