最近做了一个需求,报表月度跑批,第一次做还是挺紧张刺激的。
话不多说,直接码上。
job任务
DECLARE
job NUMBER;
begin
dbms_job.submit(job => job, --job名称
--what赋值 job绑定存储过程 注意存储过程名称后面要加上 ';'(分号)
what => 'pkg_tppecs_service_main_dwq.p_evaluate_month_run();', --包名 . 方法名
--next_date赋值 这里是下次定时任务运行的时间 sysdate(系统时间) 每天 00:30执行
next_date => trunc(sysdate) + 0.5 / 24,
--interval赋值 定时任务间隔时间 系统时间次日 00:30执行
interval => 'trunc(sysdate+1)+0.5 / 24');
commit;
end;
下面是具体执行跑批sql逻辑
create or replace package body pkg_tppecs_service_main_dwq is
--增值服务满意度评价情况表-月报
procedure p_evaluate_month_run is
v_date date;
begin
v_date := trunc(sysdate);
p_evaluate_month_run(v_date);
end;--增值服务满意度评价情况表-月报
procedure p_evaluate_month_run(p_date date) is
v_date date;
v_front_month_first_day date;
v_ratio number(4, 2);
begin
----上月第一天
v_front_month_first_day := trunc(add_months(p_date, -1), 'mm');
select trunc(p_date, 'mm') into v_date from dual;
----如果是每月一号则进行跑批 (p_date 当前时间 v_date 每月一号)
if p_date = v_date then
--if 1 = 1 then
for v_c in (select t1.*, o.organ_id,o.shorter_name
from (select --t.organ_id,
t.oragn_two,
sum(t.is_send) pushed_number, --已推送问卷数量
sum(t.advise) evaluation_number, -- 客户评价数量
sum(t.very_good_number) very_good_number, -- 非常满意
sum(t.good_number) good_number, --
sum(t.ordinary_number) ordinary_number, --
sum(t.dissatisfied_number) dissatisfied_number --
,
t.ym
from (select a.apply_id,
a.organ_id, ---机构id
o1.oragn_two,
to_char(a.finish_date, 'yyyy-mm') ym,
--a.finish_date ym,
decode(f.service_advise, null, 0, 1) advise,
decode(s.send_id, null, 0, 1) is_send, --是否推送
decode(f.service_appraise, 1, 1, 0) very_good_number, --非常满意
decode(f.service_appraise, 2, 1, 0) good_number, --2满意
decode(f.service_appraise, 3, 1, 0) ordinary_number, --3一般
decode(f.service_appraise, 4, 1, 0) dissatisfied_number --4不满意
from tppecs_user_apply a,
tppecs_service_feedback f,
tppecs_imp_organ o1,--机构
kf_rule_satisfaction_send s
where a.apply_id = f.apply_id
and a.apply_id = s.apply_id(+)
and a.organ_id = o1.organ_id
and a.finish_date > v_front_month_first_day --时间
and a.finish_date < v_date) t --时间
group by t.oragn_two, t.ym) t1,
tppecs_imp_organ o
where t1.oragn_two = o.organ_id
and o.status = 1
and o.organ_id != '1'
) loop
v_ratio := (v_c.very_good_number + v_c.good_number) / v_c.evaluation_number;
--dbms_output.put_line(ratio);
insert into kf_satisfaction_evaluate
(record_id, --记录id
organ_id, --机构id
organ_name, --机构名称
pushed_number, -- 已推送问卷数量
evaluation_number, --客户评价数量
very_good_number, --非常满意
good_number, --满意
ordinary_number, --一般
dissatisfied_number, --不满意
satisfaction, --满意率
DATA_STATUS, --是否有效
fcd, --时间
create_date
)
values
(kf_satisfaction_evaluate_id.nextval,
v_c.organ_id,
v_c.shorter_name,
v_c.pushed_number,
v_c.evaluation_number,
v_c.very_good_number,
v_c.good_number,
v_c.ordinary_number,
v_c.dissatisfied_number,
v_ratio,
1,
sysdate,
v_c.ym);
end loop;
end if;
commit;
end;
end pkg_tppecs_service_main_dwq;