--按年查询
select * from table1 where year(时间)=2013
--按月查询
select * from table1 where convert(varchar(7),时间,23)='2014-03'
--按周查询 2013年第五周
select * from table1 where year(时间)=2013 and datepart(dw,'时间')=5
------解决方案--------------------
select * from (
select sum(value) value ,month(count_date) m,year(count_date) y
from table1
group by cube(month(count_date),year(count_date) )
)f
where f.m is null
with all_date as(
select DATEADD(day,number,@begindate) as eachdate,
case when @datepart='year' then DATEPART(year,DATEADD(day,number,@begindate))
when @datepart='month' then DATEPART(month,DATEADD(day,number,@begindate))
when @datepart='day' then DATEPART(day,DATEADD(day,number,@begindate))
when @datepart='week' then DATEPART(week,DATEADD(day,number,@begindate)) end as eachdatepart
from master..spt_values where type='P' and number<=DATEDIFF(day,@begindate,@enddate))
select min(eachdate) as begindate,max(eachdate) as enddate
from all_date
group by eachdatepart,YEAR(eachdate)
order by begindate