在SQLServer中按照月份查询该月有多少条数据(近三个月)
DATENAME() 截取时间中的年,月,日
select t.time,COUNT(*)as sum from (select ( DATENAME(year, datetime) + '-' + DATENAME(MONTH,datetime))as time,COUNT(*) as number From CeShi twhere 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) tgroup by t.time
