由于部分企業數據庫從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$
DECLAREc_record record;BEGINdelete from sys_statlog_month where statistics_time = monthTime;for c_record inwith used_set as (SELECTb.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_5FROM sys_statlog bWHERE 1 = 1and 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 != 999GROUP 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 a1left join used_set a2 on a1.id=a2.user_idwhere a1.status in ('1','2')union ALLselect 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 a1join used_set a2 on a1.id=a2.user_idwhere a1.status= '0'loopinsert 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 VOLATILECOST 100---下面sql演示刷2023年11月份的數據
select f_sys_statlog_month('2023-11-01','2023-12-01','202311');