1. 程式人生 > >SQL按日、周、月、年統計

SQL按日、周、月、年統計

1、按日統計

select convert(varchar(13),RingTime,120) as RingTime,count(*) as DialCount
  from D_CallRecords (nolock)  group by convert(varchar(13),RingTime,120)

2、按周統計

select datename(dw,dateadd(dd,-1,D_CallRecords.RingTime)) as RingTime from D_CallRecords 
 group by datename(dw,dateadd(dd,-1,D_CallRecords.RingTime)) 

3、按月統計

 select day([RingTime]) as RingTime from D_CallRecords (nolock)  where  year([RingTime]) =2014 and month([RingTime]) =6 group by day([RingTime]) 

4、按年統計

 select month([RingTime]) as Months from D_CallRecords (nolock)  where  year([RingTime]) =2014  group by month([RingTime])