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 ...
随机推荐
- ASP.NET Core的身份认证框架IdentityServer4--(3)令牌服务配置访问控制跟UI添加
使用密码保护API OAuth 2.0 资源所有者密码授权允许一个客户端发送用户名和密码到IdentityServer并获得一个表示该用户的可以用于访问api的Token. 该规范建议仅对" ...
- 使用tdload工具将本地数据导入到Teradata数据库中
想把本地的数据文件(比如txt.csv)中的数据导入到Teradata虚拟机中的表中.既可以使用Teradata Assistant中的import功能,也可以使用fastload导入,前者的缺点是一 ...
- C语言_结构体变量指针做函数参数的使用案例
# include <stdio.h> # include <stdlib.h> # include <string.h> # include <malloc ...
- MysqL 主从事务数据安全之sync_binlog
sync_binlog:是MySQL 的二进制日志(binary log)同步到磁盘的频率(刷新二进制日志到磁盘),默认是0,意味着mysql并不刷新,由操作系统自己决定什么时候刷新缓存到持久化设置, ...
- TP3.2 中使用 PHPMailer 发送邮件
第一步.添加PHPMailer类库 http://pan.baidu.com/s/1o7Zc7V0 第二步.添加发送邮件函数 在common目录中的公共函数文件加入函数 <?php /***** ...
- Entity Framework——配置文件设置
可以使用配置文件或代码(EF6起)配置EF框架. 一.使用配置文件 安装Entity Framework自动生成的配置 当使用VS的NuGet自动安装Entity Framework(本文使用6.2. ...
- lower_bound()返回值
lower_bound()函数实现功能就是二分查找,函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置.如果所有元素都小于val,则 ...
- hdu 2018递推
第n月的牛的数量由第n-1个月的老牛加上n-1个月新生的小牛,得到公式F(n)=F(n-1)+F(n-3) AC代码: #include<cstdio> const int maxn=55 ...
- spring oauth2 ,spring security整合oauth2.0 JdbcTokenStore实现 解决url-pattern .do .action
参考以下两个文章: http://www.cnblogs.com/0201zcr/p/5328847.html http://wwwcomy.iteye.com/blog/2230265 web.xm ...
- Linux的内核和权限
1.内核包括的子系统是 : 进程管理系统 . 内存管理系统 . I/O管理系统 和文件管理系统 等四个子系统. 2.Linux系统中某个可执行文件属于root并且有setid,当一个普通用户 mik ...