动态merge into

 

CREATE OR REPLACE PROCEDURE PROC_USSDUSER
AS
  

    query_str      varchar2(4000);

   stime           VARCHAR2(20);
   yestime           VARCHAR2(20);
   rq             VARCHAR2(20);
   area           VARCHAR2(4);
   area1           VARCHAR2(4);
   monthday          INTEGER;

BEGIN

     stime:=trim(to_char(add_months(sysdate,-1),'yyyy-mm'));
     yestime:=trim(to_char(add_months(sysdate,-2),'yyyy-mm'));
     rq:=stime||'-01';
     area:='m'||to_number(to_char(add_months(sysdate,-1),'mm'));
     area1:='m'||to_number(to_char(add_months(sysdate,-2),'mm'));
     monthday := trunc(sysdate,'mm')-add_months(trunc(sysdate,'mm'),-1);

             --会话次数
              query_str:= 'merge into ussd_month_user d
                    using (
                         SELECT t.connectchannel conn,t.DISTRICT,sum(SessionCount) counter
                          FROM    UssdDayPhoneTable_Partition partition ('||area||') T
                          WHERE TO_CHAR(T.STAMP,''YYYY-MM'')='''||stime||'''
                          group by t.connectchannel,t.DISTRICT
                    ) tmpdata
                     on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''')
                    when matched then update set d.SessionCount=(tmpdata.counter)
                    when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,0,0,0,0,decode(tmpdata.counter,null,0,tmpdata.counter),0,0,0)';
              EXECUTE IMMEDIATE query_str ;
           ---交互次数
                query_str:= 'merge into ussd_month_user d
                    using (
                         SELECT t.connectchannel conn,t.DISTRICT, sum(INTERACTCOUNT) counter
                          FROM    UssdDayPhoneTable_Partition partition ('||area||') T
                          WHERE TO_CHAR(T.STAMP,''YYYY-MM'')='''||stime||'''
                          group by t.connectchannel,t.DISTRICT
                    ) tmpdata
                     on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''')
                    when matched then update set d.INTERCOUNT=(tmpdata.counter)
                    when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,0,0,0,0,0,decode(tmpdata.counter,null,0,tmpdata.counter),0,0)';
              EXECUTE IMMEDIATE query_str ;
           ---时长
               query_str:= 'merge into ussd_month_user d
                    using (
                         SELECT t.connectchannel conn,t.DISTRICT,sum(TimeSpan) counter
                          FROM    UssdDayPhoneTable_Partition partition ('||area||') T
                          WHERE TO_CHAR(T.STAMP,''YYYY-MM'')='''||stime||'''
                          group by t.connectchannel,t.DISTRICT
                    ) tmpdata
                     on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''')
                    when matched then update set d.TimeSpan=(tmpdata.counter)
                    when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,0,0,0,decode(tmpdata.counter,null,0,tmpdata.counter),0,0,0,0)';
           EXECUTE IMMEDIATE query_str ;
          --每天使用用户数
                   query_str:= 'merge into ussd_month_user d
                    using (
                         SELECT t.connectchannel conn,t.DISTRICT,count(msisdn) counter
                          FROM    UssdDayPhoneTable_Partition partition ('||area||') T
                          WHERE TO_CHAR(T.STAMP,''YYYY-MM'')='''||stime||'''
                          group by t.connectchannel,t.DISTRICT
                    ) tmpdata
                     on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''')
                    when matched then update set d.dayuser=trunc(tmpdata.counter/'||monthday||')
                    when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,0,0,0,0,0,0,trunc(tmpdata.counter/'||monthday||'),0)';
             EXECUTE IMMEDIATE query_str ;
           --新用户数
                query_str:= 'merge into ussd_month_user d
                    using (
                         SELECT t.connectchannel conn,t.DISTRICT,count(msisdn) counter
                          FROM    UssdDayPhoneTable_Partition partition ('||area||') T
                          WHERE TO_CHAR(T.STAMP,''YYYY-MM'')='''||stime||'''
                          and NewOrOld=1
                          group by t.connectchannel,t.DISTRICT
                    ) tmpdata
                     on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''')
                    when matched then update set d.newuser=tmpdata.counter
                    when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,tmpdata.counter,0,0,0,0,0,0,0)';
           EXECUTE IMMEDIATE query_str ;
             --月不重复用户数
                  query_str:= 'merge into ussd_month_user d
                    using (
                         SELECT t.connectchannel conn,t.DISTRICT,count(distinct msisdn) counter
                          FROM    UssdDayPhoneTable_Partition partition ('||area||') T
                          WHERE TO_CHAR(T.STAMP,''YYYY-MM'')='''||stime||'''
                          group by t.connectchannel,t.DISTRICT
                    ) tmpdata
                     on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''')
                    when matched then update set d.distinctuser=tmpdata.counter
                    when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,0,tmpdata.counter,0,0,0,0,0,0)';
          EXECUTE IMMEDIATE query_str ;
            --上月留存用户
               query_str:= 'merge into ussd_month_user d
                    using (SELECT connectchannel conn,DISTRICT,count(distinct msisdn) counter FROM (select connectchannel,DISTRICT,msisdn from UssdDayPhoneTable_Partition partition ('||area||')
                          WHERE TO_CHAR(STAMP,''YYYY-MM'')='''||stime||''' intersect select connectchannel,DISTRICT,msisdn from UssdDayPhoneTable_Partition partition ('||area1||')
                          where to_char(stamp,''yyyy-mm'')='''||yestime||''') group by connectchannel,DISTRICT) tmpdata on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''' )
                    when matched then update set d.LastMonthUser=tmpdata.counter when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,0,0,tmpdata.counter,0,0,0,0,0)';
           
             EXECUTE IMMEDIATE query_str ;
            ----总用户数ALLUSER
             query_str:= 'merge into ussd_month_user d
                    using (
                         SELECT t.connectchannel conn,t.DISTRICT,COUNT(distinct msisdn) counter
                          FROM    UssdDayPhoneTable_Partition T
                          WHERE TO_CHAR(T.STAMP,''YYYY-MM'')<='''||stime||'''
                          group by t.connectchannel,t.DISTRICT
                    ) tmpdata
                     on (d.DISTRICT=tmpdata.DISTRICT and d.connectchannel=tmpdata.conn and to_char(d.stamp,''yyyy-mm-dd'')='''||rq||''' )
                    when matched then update set d.ALLUSER=(tmpdata.counter)
                    when not matched then insert values(tmpdata.DISTRICT,to_date('''||rq||''',''yyyy-mm-dd''),tmpdata.conn,0,0,0,0,0,0,0,tmpdata.Counter)';
             EXECUTE IMMEDIATE query_str ;
       
          

END;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值