pgsql存储过程

由于部分企业数据库从aws迁移到腾讯云,导致有一个定时任务(从详情表汇总数据到统计表中)错过了触发,所以这部分企业的数据需要触发重新刷一下,但是又有规定白天不允许上线,只能把定时任务的逻辑用存储过程(函数)实现一遍,通过这种方式把数据刷正确。下面是完整的存储过程示例:

 --删除储存过程
 --DROP FUNCTION f_sys_statlog_month(text,text,text) 
 --新建储存过程
CREATE OR REPLACE FUNCTION "public"."f_sys_statlog_month"(beginTime TEXT,endTime TEXT,monthTime TEXT)
  RETURNS "pg_catalog"."void" AS $BODY$ 
DECLARE
     c_record record;
     BEGIN
      delete from sys_statlog_month where statistics_time = monthTime;
      for c_record in
       with used_set as (
                            SELECT
                            b.user_id,
                            sum(case when b.client_type='1' THEN 1 else 0 end) as client_1, 
                            sum(case when b.client_type='2' THEN 1 else 0 end) as client_2, 
                            sum(case when b.client_type='4' THEN 1 else 0 end) as client_4,
                            sum(case when b.client_type='5' THEN 1 else 0 end) as client_5
                            FROM sys_statlog b
                            WHERE 1 = 1
                            and b.occur_time >= to_date(beginTime,'yyyy-MM-dd')
                            AND b.occur_time < to_date(endTime,'yyyy-MM-dd')
                            and b.operate_type IN ('1', '2')
                            and b.user_id != 999
                            GROUP BY b.user_id
                        )
                         select id,
                            COALESCE(client_1, 0) as client_1,
                            COALESCE(client_2, 0) as client_2,
                            COALESCE(client_4, 0) as client_4,
                            COALESCE(client_5, 0) as client_5 from sys_employee a1
                            left join used_set a2 on a1.id=a2.user_id
                            where a1.status in ('1','2')
                         union ALL
                            select id,
                                 COALESCE(client_1, 0) as client_1,
                                 COALESCE(client_2, 0) as client_2,
                                 COALESCE(client_4, 0) as client_4,
                                 COALESCE(client_5, 0) as client_5 
                                from sys_employee a1
                                join used_set a2 on a1.id=a2.user_id
                                where a1.status= '0'
                        loop
                         insert into sys_statlog_month
                         (
                                 id, 
                                 user_id,
                                 creator_id,
                                 create_time,
                                 client_type_1,
                                 client_type_2,
                                 client_type_4,
                                 client_type_5,
                                 statistics_time
                         )
                         values 
                         (
                                cast(RANDOM() * 10000000000000 as int8), 
                                c_record.id,
                                -10000,
                                now(), 
                                c_record.client_1,
                                c_record.client_2, 
                                c_record.client_4,
                                c_record.client_5,
                                monthTime
                        );
                end loop;
                END; 
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

---下面sql演示刷2023年11月份的数据
select f_sys_statlog_month('2023-11-01','2023-12-01','202311');

相关推荐

  1. pgsql存储过程

    2023-12-09 16:16:07       33 阅读
  2. 创建存储过程,与存储过程调用

    2023-12-09 16:16:07       26 阅读
  3. 存储过程与视图

    2023-12-09 16:16:07       37 阅读
  4. SQL 存储过程&触发器

    2023-12-09 16:16:07       44 阅读
  5. mysql_存储过程

    2023-12-09 16:16:07       39 阅读
  6. oracle 存储过程模板

    2023-12-09 16:16:07       34 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-09 16:16:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-09 16:16:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-09 16:16:07       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-09 16:16:07       20 阅读

热门阅读

  1. configure脚本的常用参数

    2023-12-09 16:16:07       40 阅读
  2. LambdaUpdateWrapper表达式新写法解释

    2023-12-09 16:16:07       35 阅读
  3. 蛋白质序列FeatureDict转化为TensorDict

    2023-12-09 16:16:07       32 阅读
  4. BFC(Block Formatting Contexts)块级格式化上下文

    2023-12-09 16:16:07       33 阅读
  5. SQL 语法

    2023-12-09 16:16:07       32 阅读
  6. C++使用模板的注意事项

    2023-12-09 16:16:07       28 阅读
  7. 比较不同聚类方法的评估指标

    2023-12-09 16:16:07       36 阅读
  8. SpringBoot基础系列:工具类使用

    2023-12-09 16:16:07       36 阅读