SqlServer 统计用户量实例(按年,月,日分组)
知识在于积累。好记星不如烂笔头。代码如下:
1 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetUsersOfStatistical]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) 2 drop procedure [dbo].[GetUsersOfStatistical] 3 go 4 create proc GetUsersOfStatistical 5 ( 6 @TimeType varchar(50), 7 @Wherestr Varchar(3000) 8 ) 9 as 10 begin 11 declare @sqlstr varchar(max) 12 if(@TimeType='Year') 13 begin 14 set @sqlstr='select datename(year,CreateTime) as creatime,sum(case Str3 when 1 then 1 when 3 then 1 when 5 then 1 else 0 end ) as ygb ,sum(case Str3 when 0 then 1 when 2 then 1 when 4 then 1 else 0 end ) as wgb ,sum(1) as allcount from egb_user_audit ' +@Wherestr +' group by datename(year,CreateTime)' 15 end 16 else if(@TimeType='Month') 17 begin 18 set @sqlstr='select datename(year,CreateTime)+datename(month,CreateTime) as creatime,sum(case Str3 when 1 then 1 when 3 then 1 when 5 then 1 else 0 end ) as ygb ,sum(case Str3 when 0 then 1 when 2 then 1 when 4 then 1 else 0 end ) as wgb ,sum(1) as allcount from egb_user_audit ' +@Wherestr +' group by datename(year,CreateTime)+datename(month,CreateTime)' 19 end 20 else 21 begin 22 set @sqlstr='select Convert(varchar(10),CreateTime,120)as creatime,sum(case Str3 when 1 then 1 when 3 then 1 when 5 then 1 else 0 end ) as ygb ,sum(case Str3 when 0 then 1 when 2 then 1 when 4 then 1 else 0 end ) as wgb ,sum(1) as allcount from egb_user_audit ' +@Wherestr +' group by Convert(varchar(10),CreateTime,120)' 23 end 24 exec(@sqlstr); 25 end 26 27 exec GetUsersOfStatistical '','where Str2 in (10001)'
千里之行始于足下