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 ...
随机推荐
- 洛谷P1896 [SCOI2005]互不侵犯King【状压DP】
题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 输入格式: 只有一行,包含两个数N,K ...
- 为何要部署IPV6
·IPv4的局限性: 1.地址空间的局限性:IP地址空间的危机由来已久,并正是升级到IPv6的主要动力. 2.安全性:IPv4在网络层没有安全性可言,安全性一直被认为是由网络层以上的层负责. ...
- spring,property not found on type
刚开始学spring mvc ,遇到这个报错. jsp页面 user 实体类 public class User { private String name; private int age; pri ...
- Python自动化--语言基础7--操作日志、加密、发送邮件、线程、生产者消费者
1.操作日志 logging.basicConfig:日志的统一处理器,对日志的输出格式和方式做配置日志级别等级CRITICAL > ERROR > WARNING > INFO & ...
- JAVA受检异常和非受检异常举例
受检异常和非受检异常(运行时异常)举例 RuntimeException(即非受检异常): RuntimeException在默认情况下会得到自动处理,所以通常用不着捕获RuntimeExceptio ...
- ListIterator的使用
package cn.lonecloud.Iterator; import java.util.ArrayList; import java.util.ListIterator; public cla ...
- 下载visual studio 的离线包
我们知道,visual studio是微软发布的软件开发的集成平台,十分好用,可以说是目前最好用的软件编辑工具. 最新版的是visual studio 2017,但是在微软的官网上只支持下载在线安装器 ...
- java读取数据写入txt文件并将读取txt文件写入另外一个表
package com.xsw.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.F ...
- 排序算法整理(python version)
import random import time def bubble_sort(a): n=len(a) while n>1: for i in range(n-1): if a[i]> ...
- Android WebView的缓存方式分析
WebView的缓存可以分为(1)页面缓存和(2)数据缓存. 页面缓存是指当WebView加载一个网页时的html.JS.CSS等页面或者资源数据.这些缓存资源是由于浏览器的行为而产生,开发者只能通过 ...