create or replace procedure pro_bi_hwfx (firstday in varchar2,lastday in varchar2) is
cnt integer;
cnt2 integer;
begin
select count(*) into cnt from tab where tname='TB_LZW_SC_TMP1';
select count(*) into cnt2 from tab where tname='TB_LZW_SC_TMP';
if cnt>0 then
execute immediate 'truncate table tb_lzw_sc_tmp1 drop storage';
else
execute immediate
'create table tb_lzw_sc_tmp1
(
statis_date date ,
servnumber varchar(11) ,
duration numeric(32)
)';
end if;
set serveroutput on;
if cnt2>0 then
execute immediate 'truncate table tb_lzw_sc_tmp drop storage';
else
execute immediate 'create table tb_lzw_sc_tmp
(
SERVNUMBER VARCHAR2(11),
DURATION_01 NUMBER,
DURATION_02 NUMBER,
DURATION_03 NUMBER,
DURATION_04 NUMBER,
DURATION_05 NUMBER,
DURATION_06 NUMBER,
DURATION_07 NUMBER,
DURATION_08 NUMBER,
DURATION_09 NUMBER,
DURATION_10 NUMBER,
DURATION_11 NUMBER,
DURATION_12 NUMBER,
DURATION_13 NUMBER,
DURATION_14 NUMBER,
DURATION_15 NUMBER,
DURATION_16 NUMBER,
DURATION_17 NUMBER,
DURATION_18 NUMBER,
DURATION_19 NUMBER,
DURATION_20 NUMBER,
DURATION_21 NUMBER,
DURATION_22 NUMBER,
DURATION_23 NUMBER,
DURATION_24 NUMBER,
DURATION_25 NUMBER,
DURATION_26 NUMBER,
DURATION_27 NUMBER,
DURATION_28 NUMBER,
DURATION_29 NUMBER,
DURATION_30 NUMBER,
DURATION_31 NUMBER
)';
end if;
for vd in firstday..lastday loop
insert into tb_lzw_sc_tmp1
select /*+ parallel(a, 15) */
statis_date ,
trim(serv_number),
sum(busy_duration+idle_duration)
from dgdm_mk.tb_mk_ls_call_day a
where statis_date = to_date(vd, 'yyyymmdd')
group by statis_date,trim(serv_number);
commit;
end loop;
execute immediate 'truncate table tb_lzw_sc_tmp drop storage';
execute immediate ('insert into tb_lzw_sc_tmp ( '
||'SERVNUMBER ,'
||'DURATION_01 ,'
||'DURATION_02 ,'
||'DURATION_03 ,'
||'DURATION_04 ,'
||'DURATION_05 ,'
||'DURATION_06 ,'
||'DURATION_07 ,'
||'DURATION_08 ,'
||'DURATION_09 ,'
||'DURATION_10 ,'
||'DURATION_11 ,'
||'DURATION_12 ,'
||'DURATION_13 ,'
||'DURATION_14 ,'
||'DURATION_15 ,'
||'DURATION_16 ,'
||'DURATION_17 ,'
||'DURATION_18 ,'
||'DURATION_19 ,'
||'DURATION_20 ,'
||'DURATION_21 ,'
||'DURATION_22 ,'
||'DURATION_23 ,'
||'DURATION_24 ,'
||'DURATION_25 ,'
||'DURATION_26 ,'
||'DURATION_27 ,'
||'DURATION_28 ,'
||'DURATION_29 ,'
||'DURATION_30 ,'
||'DURATION_31 )'
||'select /*+ parallel(a, 15) */ servnumber, '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'') then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+1 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+2 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+3 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+4 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+5 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+6 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+7 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+8 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+9 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+10 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+11 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+12 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+13 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+14 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+15 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+16 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+17 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+18 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+19 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+20 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+21 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+22 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+23 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+24 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+25 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+26 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+27 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+28 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+29 then duration else 0 end), '
||'max(case when statis_date = to_date('||firstday||', ''yyyymmdd'')+30 then duration else 0 end) '
||''
||'from tb_lzw_sc_tmp1 a '
||'group by servnumber ');
commit;
end pro_bi_hwfx;