sqlserver存储过程及临时表在统计中的应用
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存储过程及临时表在统计中的应用的更多相关文章
- Sybase:存储过程中采用临时表存储统计数据
Sybase:存储过程中采用临时表存储统计数据 作用 很有效的提升统计查询速度,对于数据量亿级.千万级多表之间关联查询,非常有效: 使用 --无需定义临时表,直接使用 --自动释放临时表 select ...
- 创建并在项目中调用SQLSERVER存储过程的简单示例
使用SQLSERVER存储过程可以很大的提高程序运行速度,简化编程维护难度,现已得到广泛应用.创建存储过程 和数据表一样,在使用之前需要创建存储过程,它的简明语法是: 引用: Create PROC ...
- [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器
本文转自:http://blog.csdn.net/nnaabbcc/article/details/7967761 存储过程调用不同数据库的数据 在存储过程调用不同数据库的数据该如何做,比如在存储过 ...
- Sqlserver 存储过程中结合事务的代码
Sqlserver 存储过程中结合事务的代码 --方式一 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ ...
- 同时执行2个存储过程,2个SP中分别有相同的临时表名,会有冲突吗?
同时执行2个存储过程,2个SP中分别有相同的临时表名,会有冲突吗?答案:不会 这就可以在以后写存储过程的时候统一临时表名了. alter procedure sp_01 as begin create ...
- 【转载】Sqlserver存储过程中使用Select和Set给变量赋值
Sqlserver存储过程是时常使用到的一个数据库对象,在存储过程中会使用到Declare来定义存储过程变量,定义的存储过程变量可以通过Set或者Select等关键字方法来进行赋值操作,使用Set对存 ...
- 搜索sqlserver 存储过程中的关键字
搜索sqlserver 存储过程中的关键字 select * from sys.all_sql_modules where definition like '%SP_NAME%'
- SQLSERVER存储过程语法详解
CREATE PROC [ EDURE ] procedure_name [ ; number ] [ { @parameter data_type } [ VARYING ] [ = default ...
- SqlServer存储过程详解
SqlServer存储过程详解 1.创建存储过程的基本语法模板: if (exists (select * from sys.objects where name = 'pro_name')) dro ...
随机推荐
- 原生ajax写的上拉加载
上拉加载的思路 1 上拉加载是要把屏幕拉到最底部的时候触发ajax事件请求数据 2.所有要获取屏幕的高度 文档的高度 和滚动的高度 下面的代码是已经做好了兼容的可以直接拿来用 Javascript: ...
- 浅谈PHP答题卡识别(一)
最近期末考试考完了,我们也要放寒假了.于是突发奇想,想用PHP写一个答题卡识别程序.已经实现了一些,现分享给大家. 具体的步骤如下: 上传答题卡=>图片二值化(已实现)=>寻找定位点(已实 ...
- levmar ndk 编译
levmar是一个强大的和高效率的C/C++库,采用Levenberg - 马奎德(LM)优化算法, 主要是为了解决非线性最小二乘问题.官网是:http://users.ics.forth. ...
- 01 深入理解JVM的内存区域
先来看看JVM运行时候的内存区域,如下图: 大多数 JVM 将内存区域划分为 Heap(堆).方法区.Stack(栈).本地方法栈.程序计数器.其中 Heap 和 方法区 是线程共享的,Stack.本 ...
- javascript 函数详解
一.函数的一些基础概念: 1.js中的函数使用function来声明. 2.关于return: 2.1 函数在执行到return语句后悔立即停止并退出,return后面的代码永远不会得到执行: 2. ...
- DOCKER 无法获取使用宿主机DNS 的原因,解决方法
今天在公司服务器上部署项目,遇到一个大坑.接口怎么请求都不同,宿主机DNS已经改了.宿主可以请求,找了半天,原来是DOCKER 没有获取到主机的DNS 进去DOCKER解析不了域名 指定DNS 启动也 ...
- Spark第一个应用程序
首先要对源码进行编译,生成对应hadoop版本的spark开发程序jar包,上篇已经写了具体的过程,这里不再赘述. 在安装spark的机器上,下载eclipse-java-x86_64版本,将spar ...
- 【前端】Vue2全家桶案例《看漫画》之四、漫画页
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_app_4.html 项目github地址:https://github.com/shamoyuu/ ...
- linux内核体系结构
linux内核第一记 1.linux体系结构 从上图可知,Linux分为:用户空间和内核空间.内核空间和用户空间是程序执行的两种不同的状态,通过系统调用和硬件中断能够完成从用户空间到内核空间的 ...
- Android 进程常驻、进程守护、进程保活技术的总结
转载自:http://blog.csdn.net/marswin89/article/details/50917098 这是一个轻量级的库,配置几行代码,就可以实现在Android上实现进程常驻,也就 ...