开发者社区> 小5聊基础> 正文

【Sql Server】存储过程通过作业定时执行按天统计记录

简介: 通过前两篇文章的学习,我们已经对创建表、存储过程、作业等功能点有所了解 本次将结合前面所学习的知识点,创建统计表以及结合作业定时按天以及实时统计域名各个长度的记录值
+关注继续查看
作者:小5聊基础
简介:一只喜欢全栈方向的程序员,欢迎咨询,尽绵薄之力答疑解惑
编程原则:Write Less Do More

统计效果
image.png

【创建统计表】
以时间字符串作为主键,确保每天只能由唯一一条统计记录

create table domain_count_day(
  id int identity(1,1),
  length_three_count int,
  length_four_count int,
  length_five_count int,
  length_six_count int,
  length_seven_count int,
  length_eight_count int,
  create_time varchar(10) primary key
)

【临时表】
创建临时的同时,需要加一个判断
如果临时表存在,那么可以根据自己业务情况,先删除临时表,然后再通过select into的方法将查询到数据直接插入到临时表里,省了创建临时表设置的字段和类型

使用说明
1)使用object_id方法
2)特别要注意,一定要是(N'tempdb..#temp',N'U')格式,否则容易出现判断不准的情况
3)删除临时表使用drop table关键词,和删除表操作一样
4)select * from into 临时表 from(目标查询) as 别名

--创建临时表
if object_id(N'tempdb..#temp',N'U') is not null
begin
    --删除临时表
    drop table #temp2    
end
select * into #temp2 from(
    select 
    domain_length,
    count(1) as count_length
    from dbo.domain_table
    where convert(varchar(10),create_time,120)=@time_rows
    group by domain_length
    --order by domain_length asc
) as aaa

【创建存储过程】

逻辑说明
1)定义今天和昨天的记录值变量
用于判断当前时间节点是否生成了记录,有记录则更新操作,没有记录则添加操作
2)再定义今天和昨天的时间值变量
用于过滤筛选今天和昨天的统计记录
3)再定义对应38位长度的值变量
4)创建临时表
以今天或昨天时间为筛选记录,以域名长度为分组,进行记录统计,并设置域名长度和总数两个字段,追加到临时表里
5)通过38长度为筛选条件,赋值到对象变量里
6)最后根据是否存在统计记录进行添加和更新

--drop proc countDomainValueDay;
create procedure countDomainValueDay
as
begin 
    
    declare @prev_time_rows int
    declare @time_rows int
    declare @length_three_count int
    declare @length_four_count int
    declare @length_five_count int
    declare @length_six_count int
    declare @length_seven_count int
    declare @length_eight_count int
    declare @day_time_prev varchar(50)
    declare @day_time varchar(50)
    
    -----当前时间的前一天-----
    set @day_time_prev=convert(varchar(10),dateadd(day,-1,getdate()),120)
    
    --创建临时表
    if object_id(N'#temp') is not null
    begin
        --删除临时表
        drop table #temp    
    end
    select * into #temp from(
        select 
        domain_length,
        count(1) as count_length
        from dbo.domain_table
        where convert(varchar(10),create_time,120)=@day_time_prev
        group by domain_length
        --order by domain_length asc
    ) as aaa

    select @length_three_count=count_length from #temp where domain_length=3
    select @length_four_count=count_length from #temp where domain_length=4
    select @length_five_count=count_length from #temp where domain_length=5
    select @length_six_count=count_length from #temp where domain_length=6
    select @length_seven_count=count_length from #temp where domain_length=7
    select @length_eight_count=count_length from #temp where domain_length=8
    
    --判断日统计记录是否存在 - 前一天
    select @prev_time_rows=count(1) from domain_count_day
    where convert(varchar(10),create_time,120)=@day_time_prev
    
    if @prev_time_rows<=0
    begin
        insert into domain_count_day(length_three_count,length_four_count,length_five_count,length_six_count,length_seven_count,length_eight_count,create_time)
        values(@length_three_count,@length_four_count,@length_five_count,@length_six_count,@length_seven_count,@length_eight_count,@day_time_prev)
    end
    else 
    begin
        update domain_count_day set
        length_three_count=@length_three_count,
        length_four_count=@length_four_count,
        length_five_count=@length_five_count,
        length_six_count=@length_six_count,
        length_seven_count=@length_seven_count,
        length_eight_count=@length_eight_count
        where create_time=@day_time_prev
    end
    -----/当前时间的前一天-----
    
    
    -----当前时间-----
    set @day_time=convert(varchar(10),getdate(),120)
    
    --创建临时表
    if object_id(N'#temp2') is not null
    begin
        --删除临时表
        drop table #temp2    
    end
    select * into #temp2 from(
        select 
        domain_length,
        count(1) as count_length
        from dbo.domain_table
        where convert(varchar(10),create_time,120)=@@day_time
        group by domain_length
        --order by domain_length asc
    ) as aaa

    select @length_three_count=count_length from #temp2 where domain_length=3
    select @length_four_count=count_length from #temp2 where domain_length=4
    select @length_five_count=count_length from #temp2 where domain_length=5
    select @length_six_count=count_length from #temp2 where domain_length=6
    select @length_seven_count=count_length from #temp2 where domain_length=7
    select @length_eight_count=count_length from #temp2 where domain_length=8
    
    --判断日统计记录是否存在 - 当前
    select @time_rows=count(1) from domain_count_day
    where convert(varchar(10),create_time,120)=@day_time
    
    if @time_rows<=0
    begin
        insert into domain_count_day(length_three_count,length_four_count,length_five_count,length_six_count,length_seven_count,length_eight_count,create_time)
        values(@length_three_count,@length_four_count,@length_five_count,@length_six_count,@length_seven_count,@length_eight_count,@day_time)
    end
    else 
    begin
        update domain_count_day set
        length_three_count=@length_three_count,
        length_four_count=@length_four_count,
        length_five_count=@length_five_count,
        length_six_count=@length_six_count,
        length_seven_count=@length_seven_count,
        length_eight_count=@length_eight_count
        where create_time=@day_time
    end
end

【开启作业】
1)作业基本信息
image.png

2)步骤设置 数据库这里容易选错,如果没注意的话
image.png

3)设置计划 可以使用上一篇文章设置好的定时计划
image.png

4)开始作业
image.png

【统计效果】
从统计效果可以得到一个有意思的结论
1)首位出现的概率稍微小一点
2)越靠近中间,值就越对称接近

image.png

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
【Sql Server】存储过程通过定时执行添加记录作业
通过上篇了解了什么是存储过程,创建存储过程的方法,以及调用存储过程的方法 本次将通过数据库中的作业功能,进行定时执行存储过程,这样就可以完成我们刚开始假设的场景
87 0
【Sql Server】存储过程的创建和调用,随机添加域名记录
假设有这样一个场景 创建一个储存过程A,它执行添加一条随机产生3到8位长度的域名记录,通过定时器T1每隔1秒执行一次存储过程A 创建另一个存储过程B,它执行统计域名的长度3到8的记录数,通过定时器T2每隔1秒执行一次存储过程B
29 0
SQL Server——SQL Server存储过程与exec简单使用
SQL Server——SQL Server存储过程与exec简单使用
361 0
SQL Server存储过程详细介绍(二)
存储过程其实就是已预编译为可执行过程的一个或多个SQL语句。 通过调用和传递参数即可完成该存储过程的功能。 前面有介绍过存储过程的一些语法,但是没有详细示例,今天我们来一起研究一下存储过程。
71 0
SQL Server存储过程详细介绍(一)
存储过程其实就是已预编译为可执行过程的一个或多个SQL语句。 通过调用和传递参数即可完成该存储过程的功能。 前面有介绍过存储过程的一些语法,但是没有详细示例,今天我们来一起研究一下存储过程。
73 0
SQL Server存储过程总结
存储过程简介: 存储过程(Stored Procedure)是在大型数据库中,一组为了完成特定功能的SQL 语句集,它存储在数据库中,一次编译后永久有效,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象。在数据量特别庞大的情况下利用存储过程能达到倍速的效率提升 ​ ------来源于百度百科 存储过程的种类: 1系统存储过程 以sp_开头,用来进行系统的各项设定.取得信息.相关管理工作。 2本地存储过程 用户创建的存储过程是由用户创建并完成某一特定功能的存储过程,事实上一般所说的存储过程就是指本地存储过程。
132 0
SQL server 存储过程的建立和调用
SQL server 存储过程的建立和调用存储过程的建立和调用--1.1准备测试需要的数据库:test,数据表:物料表,采购表if not exists (select * from master.dbo.
1052 0
+关注
小5聊基础
帮助别人,成长自己!
文章
问答
文章排行榜
最热
最新
相关电子书
更多
用SQL做数据分析
立即下载
阿里云流计算 Flink SQL 核心功能解密
立即下载
Comparison of Spark SQL with Hive
立即下载