维度表更新方法 - 增量更新

本文介绍了一种针对维度表的增量更新方法,包括通过比较标识位来确定新增、修改及删除记录的过程。利用MERGE语句高效地进行数据同步,并通过不同标志位管理ETL流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

维度表更新方法- 增量更新总结:



/******批量导入,从source表到stage target表,赋相应标识位:目的是找出新增加的和经过修改的记录******/
begin
    merge into emp_t t
    using (select * from emp_s) s
    on ( t.empid=s.empid
         and t.modify_time=s.modify_time)
    when matched then
      update set t.action_flag='0'
    when not matched then
      insert  (empid,empnm,deptnm,create_time,modify_time,action_flag)
        values (s.empid, s.empnm, s.deptnm, s.create_time, s.modify_time,'4' )
    ;
    commit;
end;
 
/******找出距离最近一次ETL时间以来,由源系统新增加的记录,赋标志位为2,维度表直接Insert*********/

begin
 update emp_t set action_flag='2' where create_time>(select nvl(max(create_time),to_date('01/01/2000','DD/MM/YYYY')) as createmaxtime from emp_t where action_flag='0' );
 commit;
end;


/****************找出数据仓库已经Load到的数据,但是在源系统经过修改的记录,改标志位为1,维度表需要更新或者生成新版本;
                  改原有记录的标志位为0,出错改标志位为5,0和5的记录不经过ETL到数据仓库*********/
begin
    
 merge into emp_t t
 using ( select empid,action_flag,empnm,deptnm,create_time,modify_time,rnk from(
          select empid,action_flag,empnm,deptnm,create_time,modify_time,rank() over(partition by empid order by modify_time desc) as rnk
          from emp_t where empid in  ( select empid from emp_t where action_flag='4' )) s_tmp where rnk='1') s
 on (t.empid=s.empid
 and t.modify_time=s.modify_time)
 when matched then
    update set t.action_flag='1'
 when not matched then
     insert  (empid,empnm,deptnm,create_time,modify_time,action_flag)
        values (s.empid, s.empnm, s.deptnm, s.create_time, s.modify_time,'5' );
 commit;
    
 merge into emp_t t
 using ( select empid,action_flag,empnm,deptnm,create_time,modify_time,rnk from(
          select empid,action_flag,empnm,deptnm,create_time,modify_time,rank() over(partition by empid order by modify_time desc) as rnk
          from emp_t where empid in  ( select empid from emp_t where action_flag='4' )) s_tmp where rnk='2') s
 on (t.empid=s.empid
 and t.modify_time=s.modify_time)
 when matched then
    update set t.action_flag='0'
 when not matched then
     insert  (empid,empnm,deptnm,create_time,modify_time,action_flag)
        values (s.empid, s.empnm, s.deptnm, s.create_time, s.modify_time,'5' );
 commit;
end;


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dbLenis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值