SQL按照月份查询该月有多少条数据
程序员文章站
2022-07-15 11:14:02
...
在SQLServer中按照月份查询该月有多少条数据(近三个月)
DATENAME() 截取时间中的年,月,日
select t.time,COUNT(*)as sum from
(select ( DATENAME(year, datetime) + '-' + DATENAME(MONTH,datetime))as time,COUNT(*) as number From CeShi t
where DATENAME(year, datetime) + '-' + DATENAME(MONTH, datetime)
between DateName(year,GetDate()) +'-'+convert(varchar,(convert(int,DateName(MONTH,GetDate())-2)))
and DateName(year,GetDate()) +'-'+convert(varchar,(convert(int,DateName(MONTH,GetDate()))))
group by datetime) t
group by t.time