zoukankan      html  css  js  c++  java
  • 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)'
  • 相关阅读:
    09-23
    21-9-18日记1
    paho mqtt库使用记录
    UIS8811 的 NB-IOT 初认识(21-9-10工作记录 )
    go语言之异常捕获
    【原创】数据库基础之PostgreSQL
    【原创】Linux基础之fail2ban
    【原创】大叔经验分享(132)mac os 12启动idea失败
    【原创】大数据基础之Hadoop(3)hdfs diskbalancer
    【转】常用 Git 命令清单
  • 原文地址:https://www.cnblogs.com/ybyi/p/4536084.html
Copyright © 2011-2022 走看看