use ResourceShare
--统计使用情况
alter PROCEDURE StaSheryUse
@start datetime,
@end datetime,
@orgId int
AS
BEGIN create table #Month
(
id int IDENTITY (,) primary key not null,
[year] int not null,
[month] int not null
) --计算全文传递数 select * into #FullTextTask from (select COUNT() fulltextCount,YEAR(CreateDate) [year],MONTH(CreateDate) [month] from FullTextTask where OrganId=@orgId and ([State]= or [State]=) and (CreateDate BETWEEN @start AND @end) group by YEAR(CreateDate),MONTH(CreateDate)) a --计算数据库访问次数 select * into #DBVisitTask from (select COUNT() dbvisitCount,YEAR(CreateDate) [year],MONTH(CreateDate) [month] from DBVisitTask where OrganId=@orgId and ([State]= or [State]=) and (CreateDate BETWEEN @start AND @end) group by YEAR(CreateDate),MONTH(CreateDate)) b --计算用户数
select * into #IdentityUser from (select COUNT() userCount,YEAR(CreateTime) [year],MONTH(CreateTime) [month] from IdentityUser where OrganId=@orgId and (CreateTime BETWEEN @start AND @end) group by YEAR(CreateTime),MONTH(CreateTime)) c --计算用户登录次数
select * into #UserLoginRecord from (select COUNT() loginTimes,YEAR(LoginDate) [year],MONTH(LoginDate) [month] from UserLoginRecord r left join IdentityUser u on r.UserId=u.Id where OrganId=@orgId and (LoginDate BETWEEN @start AND @end) group by YEAR(LoginDate),MONTH(LoginDate)) d insert into #Month([year],[month]) select [year],[month] from #FullTextTask
insert into #Month([year],[month]) select [year],[month] from #DBVisitTask
insert into #Month([year],[month]) select [year],[month] from #IdentityUser
insert into #Month([year],[month]) select [year],[month] from #UserLoginRecord select * into #yearAndMonth from(select [year],[month] from #Month group by [year],[month]) a select * from (select isnull(userCount, ) UserCount,isnull(dbvisitCount, ) DBvisitCount,isnull(fulltextCount, ) FulltextCount,isnull(loginTimes, ) LoginTimes,m.[year] [Year],m.[month] [Month] from #yearAndMonth m left join #UserLoginRecord a on a.[year]=m.[year] and a.[month]=m.[month]
left join #DBVisitTask b on b.[year]=m.[year] and b.[month]=m.[month]
left join #FullTextTask c on c.[year]=m.[year] and c.[month]=m.[month]
left join #IdentityUser d on d.[year]=m.[year] and d.[month]=m.[month]) f order by [Year] desc,[Month] END execute StaSheryUse '2017-04-16','2017-04-17 23:59:59',

  这段代码,我简要地介绍下,存储过程接收了两个参数,一个开始日期,一个结束日期。根据传进来的日期范围统计全文传递数,用户注册数等。11行,创建了一个月份的临时表,包括年和月两个字段。它的数据来源于其它所有要统计的表。之后42行,定义了一个#yearAndMonth的临时表,主要是把#Month表中的重复数据过滤掉。最后把众多的统计临时表和#yearAndMonth左链接查询,即可得到结果,结果是根据年份和月份统计的二维表。

sqlserver存储过程及临时表在统计中的应用的更多相关文章

  1. Sybase:存储过程中采用临时表存储统计数据

    Sybase:存储过程中采用临时表存储统计数据 作用 很有效的提升统计查询速度,对于数据量亿级.千万级多表之间关联查询,非常有效: 使用 --无需定义临时表,直接使用 --自动释放临时表 select ...

  2. 创建并在项目中调用SQLSERVER存储过程的简单示例

    使用SQLSERVER存储过程可以很大的提高程序运行速度,简化编程维护难度,现已得到广泛应用.创建存储过程 和数据表一样,在使用之前需要创建存储过程,它的简明语法是: 引用: Create PROC ...

  3. [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器

    本文转自:http://blog.csdn.net/nnaabbcc/article/details/7967761 存储过程调用不同数据库的数据 在存储过程调用不同数据库的数据该如何做,比如在存储过 ...

  4. Sqlserver 存储过程中结合事务的代码

    Sqlserver 存储过程中结合事务的代码  --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ...

  5. 同时执行2个存储过程,2个SP中分别有相同的临时表名,会有冲突吗?

    同时执行2个存储过程,2个SP中分别有相同的临时表名,会有冲突吗?答案:不会 这就可以在以后写存储过程的时候统一临时表名了. alter procedure sp_01 as begin create ...

  6. 【转载】Sqlserver存储过程中使用Select和Set给变量赋值

    Sqlserver存储过程是时常使用到的一个数据库对象,在存储过程中会使用到Declare来定义存储过程变量,定义的存储过程变量可以通过Set或者Select等关键字方法来进行赋值操作,使用Set对存 ...

  7. 搜索sqlserver 存储过程中的关键字

    搜索sqlserver 存储过程中的关键字 select * from sys.all_sql_modules where definition like '%SP_NAME%'

  8. SQLSERVER存储过程语法详解

    CREATE PROC [ EDURE ] procedure_name [ ; number ] [ { @parameter data_type } [ VARYING ] [ = default ...

  9. SqlServer存储过程详解

    SqlServer存储过程详解 1.创建存储过程的基本语法模板: if (exists (select * from sys.objects where name = 'pro_name')) dro ...

随机推荐

  1. 捋一捋js面向对象的继承问题

    说到面向对象这个破玩意,曾经一度我都处于很懵逼的状态,那么面向对象究竟是什么呢?其实说白了,所谓面向对象,就是基于类这个概念,来实现封装.继承和多态的一种编程思想罢了.今天我们就来说一下这其中继承的问 ...

  2. return的新思考

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  3. Photoshop CC 常用快捷方法有哪些?

    Photoshop CC 常用快捷方法有哪些? 属性栏 工具栏 控制面板 绘图区 1. 多个图层如何快速居中? 在 控制面板 中选中多个图层创建 链接图层 在 工具栏 选择 移动工具 在 属性栏 点击 ...

  4. UVA - 658 最短路

    思路:通过前后两种状态建立一条边,利用Dijsktra就可以做了. 注意利用二进制优化. AC代码 #include <cstdio> #include <cmath> #in ...

  5. hdu1556 Color the ball 线段树区间染色问题

    都是老套路了,如果n=5,要把区间[1,4]染色,可以递归去染区间[1,3]和区间[4,4],如果区间相等就自加,不相等继续递归寻找对应区间. 打印结果时,把所有到达叶节点包含i的区间值相加,就是最后 ...

  6. React——diff算法

    react的diff算法基于两个假设: 1.不同类型的元素会产生不同的树 2.通过设置key,开发者能够提示那些子组件是稳定的 diff算法 当比较两个树时,react首先会比较两个根节点,接下来具体 ...

  7. svn版本管理系统出现的问题解决办法

    首先会出现下面的一个提示错误: cannot checkout from svn run 'cleanup' if it was interrupted 百度加谷歌找到的解决问题的办法各种各样,自己遇 ...

  8. Oracle 11g数据库安装和卸载教程

    Oracle11g的安装教程 同时解压缩两个zip文件,生成一个database文件夹,进入到database文件夹,点击setup 去掉安全更新的选项,直接下一步 选择创建和配置数据库,点击下一步 ...

  9. Unable to find the ncurses libraries的解决办法

    我们在更新CentOS或者Ubuntu的内核时,执行make menuconfig可能看如这样的错误: *** Unable to find the ncurses libraries or the* ...

  10. CIF、DCIF、D1分辨率是多少?

    CIF简介: QCIF全称Quarter common intermediate format.QCIF是常用的标准化图像格式.在H.323协议簇中,规定了视频采集设备的标准采集分辨率.QCIF = ...