create or replace procedure p_stat_unmatch_everyday(
  v_end_time number,--结束日期
  v_start_time number--开始日期
) isbegin  /*v_last_3 := to_char(to_date(v_op_time, 'yyyymmdd') - 3, 'yyyymmdd');*/  p_deal_partition(to_char(sysdate,'yyyymmdd'),'STAT_UNMATCH_EVERYDAY');--创建分区  execute immediate 'truncate table m_stat_unmatch_everyday_1';  --先查出连续3天出现次数排行前十的  insert into m_stat_unmatch_everyday_1
    (destinationurl, re_total)
    select *
      from (select a.destinationurl, sum(a.record_counts)
              from stat_un_match_top a
             where to_char(a.create_date, 'yyyymmdd') >= v_start_time
               and to_char(a.create_date, 'yyyymmdd') <= v_end_time
               
             --过滤掉ip地址
             and not regexp_like(a.destinationurl, '^http://[0-9]+')
             
             --过滤掉已经处理过的host
             and case
             when length(a.destinationurl) -
                  length(replace(a.destinationurl, ':', '')) > 1 then
              substr(a.destinationurl,
                     instr(a.destinationurl, '.', 1, 1) + 1,
                     instr(a.destinationurl,
                           ':',
                           instr(a.destinationurl, '.', 1, 1),
                           1) - instr(a.destinationurl, '.', 1, 1) - 1)
             else                      substr(a.destinationurl,
                             instr(a.destinationurl, '.', 1, 1) + 1,
                             instr(a.destinationurl,
                                   '/',
                                   instr(a.destinationurl, '.', 1, 1),
                                   1) - instr(a.destinationurl, '.', 1, 1) - 1)
                   end not in
           (select case
                     when length(b.destinationurl) -
                          length(replace(b.destinationurl, ':', '')) > 1 then
                      substr(b.destinationurl,
                             instr(b.destinationurl, '.', 1, 1) + 1,
                             instr(b.destinationurl,
                                   ':',
                                   instr(b.destinationurl, '.', 1, 1),
                                   1) - instr(b.destinationurl, '.', 1, 1) - 1)
                     else
                      substr(b.destinationurl,
                             instr(b.destinationurl, '.', 1, 1) + 1,
                             instr(b.destinationurl,
                                   '/',
                                   instr(b.destinationurl, '.', 1, 1),
                                   1) - instr(b.destinationurl, '.', 1, 1) - 1)
                   end
              from stat_unmatch_everyday b            )
            --如果独立处理指定的省份,加上下面这个条件
          and a.area_id = '811'
             group by a.destinationurl
            -- having count(*) >= 2
             order by sum(a.record_counts) desc)
    where rownum <= 10
     ;  commit;  execute immediate ' truncate table m_stat_unmatch_everyday_2';  --将host和排行前十的重复的那部分数据也插入到目标表(不重复的)
  insert into m_stat_unmatch_everyday_2
    (destinationurl, re_total)
    select a.destinationurl, a.record_counts
      from stat_un_match_top a
     where 
     --捞出和该host相关联的一些url
     case
             when length(a.destinationurl) -
                  length(replace(a.destinationurl, ':', '')) > 1 then
              substr(a.destinationurl,
                     instr(a.destinationurl, '.', 1, 1) + 1,
                     instr(a.destinationurl,
                           ':',
                           instr(a.destinationurl, '.', 1, 1),
                           1) - instr(a.destinationurl, '.', 1, 1) - 1)
             else                      substr(a.destinationurl,
                             instr(a.destinationurl, '.', 1, 1) + 1,
                             instr(a.destinationurl,
                                   '/',
                                   instr(a.destinationurl, '.', 1, 1),
                                   1) - instr(a.destinationurl, '.', 1, 1) - 1)
                   end in
           (select case
                     when length(b.destinationurl) -
                          length(replace(b.destinationurl, ':', '')) > 1 then
                      substr(b.destinationurl,
                             instr(b.destinationurl, '.', 1, 1) + 1,
                             instr(b.destinationurl,
                                   ':',
                                   instr(b.destinationurl, '.', 1, 1),
                                   1) - instr(b.destinationurl, '.', 1, 1) - 1)
                     else
                      substr(b.destinationurl,
                             instr(b.destinationurl, '.', 1, 1) + 1,
                             instr(b.destinationurl,
                                   '/',
                                   instr(b.destinationurl, '.', 1, 1),
                                   1) - instr(b.destinationurl, '.', 1, 1) - 1)
                   end
              from m_stat_unmatch_everyday_1 b            )    and not regexp_like(a.destinationurl, '^http://[0-9]+')
       and a.destinationurl not in(
       select destinationurl from stat_unmatch_everyday
       )
       and to_char(a.create_date, 'yyyymmdd') >= v_start_time
       and to_char(a.create_date, 'yyyymmdd') <= v_end_time
       --如果独立处理指定的省份,加上下面这个条件
       and a.area_id = '811'
    ;
  commit;
  
  --插入目标表中
  insert into stat_unmatch_everyday
    (op_time,destinationurl, re_total)
    select  to_char(sysdate,'yyyymmdd'),destinationurl, sum(re_total)
    from m_stat_unmatch_everyday_2
    group by destinationurl
   ;    commit;   exception
    when others then
      dbms_output.put_line(SQLCODE || '---' || SQLERRM);
end;
/