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. 解决windows10和ubuntu16.04双系统下时间不对的问题

    电脑安装完windows10与ubuntu16.04双系统后,Ubuntu的时间总会和Windows的时间相差8小时,原因在于windows认为BIOS时间是本地时间,Ubuntu认为BIOS时间是U ...

  2. Flag

    之前一直怕别人知道你在干什么,其实根本没人在乎你在干什么.做好你自己不就行了,现在这里将成为你的记录生活与学习的地方,尽管目前就你自己,但是你要相信,会有千万人和你在一起,大胆的往前走吧.--- 致自 ...

  3. CSS布局(六) 对齐方式

    一.水平居中: (1). 行内元素的水平居中? 如果被设置元素为文本.图片等行内元素时,在父元素中设置text-align:center实现行内元素水平居中,将子元素的display设置为inline ...

  4. GCD实现倒计时

    之前面试中,好多面试官,问使用GCD如何实现倒计时,我当时也没写过,所以一时不知道怎么说,所以结束之后,我实现一下GCD的倒计时. - (void)startTime:(UIButton *)send ...

  5. Mybatis的基本使用

    .什么是Mybatis? Mybatis:根据官方解释,MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手工设置参数以及 ...

  6. Docker系统八:Docker的存储驱动

    Docker存储驱动 1. Docker存储驱动历史 Docker目前支持的greph driver包括: AUFS device-mapper btrfs overlayfs(重点) 关于各存储驱的 ...

  7. 检验金额合法性, 只能是正数 或小数(常用js总结)

    //检验金额合法性 只能是正数 或小数 function checkMoney(fileds){ $(fileds).keyup(function () { ,}/); var txt = ''; i ...

  8. Hbase Scan的方法

    public static void main(String[] args) throws IOException { //Scan类常用方法说明 //指定需要的family或column ,如果没有 ...

  9. NLP+词法系列(二)︱中文分词技术简述、深度学习分词实践(CIPS2016、超多案例)

    摘录自:CIPS2016 中文信息处理报告<第一章 词法和句法分析研究进展.现状及趋势>P4 CIPS2016 中文信息处理报告下载链接:http://cips-upload.bj.bce ...

  10. vc6.0转vs2005中字符串的问题

    简单一点:就是project->Property->Configuration Property-->general-->Character Set:No Set即可.详细分析 ...