帶刪除的歷史拉鍊算法

drop table if exists test;
create temp table test
(
 id varchar(100)
 ,name varchar(100)
 ,state varchar(100)
 ,start_date varchar(100)
 ,end_date varchar(100)
)
distributed by (id);
comment on table test is '員工狀態表';
comment on column test.id is '員工的id';
comment on column test.name is '員工的name';
comment on column test.state is '員工的狀態';
comment on column test.start_date is '開始時間';
comment on column test.end_date is '結束時間';


insert into test values('1','A','1','20150101','99991231');
insert into test values('2','B','1','20150101','99991231');
insert into test values('3','C','1','20150101','99991231');
insert into test values('4','D','1','20150101','99991231');


drop table if exists org_test;
create temp table org_test
(
 id varchar(100)
 ,name varchar(100)
 ,state varchar(100)
)
distributed by (id);
comment on table org_test is '員工狀態表';
comment on column org_test.id is '員工的id';
comment on column org_test.name is '員工的name';
comment on column org_test.state is '員工的狀態';
insert into org_test values('1','A','1');
insert into org_test values('2','B','0');
insert into org_test values('4','D','0');
insert into org_test values('5','E','1');
select * from org_test;
--創建三張臨時表
drop table if exists test1;
drop table if exists test2;
drop table if exists test3;
create  temp table test1--存放源數據
(
  id varchar(100)
 ,name varchar(100)
 ,state varchar(100)
 ,start_date varchar(100)
 ,end_date varchar(100)
)
distributed by (id);
create  temp table test2--存放更新和新增的數據
(
  id varchar(100)
 ,name varchar(100)
 ,state varchar(100)
 ,start_date varchar(100)
 ,end_date varchar(100)
)
distributed by (id);
create  temp table test3--存放沒有變更的數據
(
  id varchar(100)
 ,name varchar(100)
 ,state varchar(100)
 ,start_date varchar(100)
 ,end_date varchar(100)
)
distributed by (id);




--第二部:將源數據加載到臨時表1中
insert into test1
select id,name,state,'20150501','99991231'
from org_test;
--第三部
--刪除目標表中的大於等於今天日期的所有數據
delete from test where start_date >='20150501';
--將修改的數據全部歡顏
update test set end_date ='99991231' where end_date ='20150501';


--第四步:將新增和修改的數據添加到臨時表2中
insert into test2
select t1.id,t1.name,t1.state,t1.start_date,t1.end_date--,t2.*
from test1     t1
left join test t2
on t1.id=t2.id
and t1.name=t2.name
and t2.state=t1.state
where t2.id is null
and t2.end_date is null;
select * from test order by 1;
select * from test3 order by 1;
--第四步:將沒有改變的數據添加到
insert into test3
select t.id,t.name,t.state,t.start_date--,t2.* 
,case when t1.id is not null and t1.end_date is not null and t.end_date ='99991231'
then '20150501'
when t2.id is  null and t2.end_date is  null and t.end_date ='99991231'
then '20150501'
else t.end_date end end_date
 from test t
 left join test2 t1
 on t1.id=t.id
 left join test1 t2
 on t.id=t2.id
 and t.name=t2.name
 and t.state=t2.state;
 --第五步:清空目標表
 truncate test;
 --第六步:將更新的數據添加到目標表中
 insert into test
 select * from test2;
 --第七部:將沒有變更的數據添加到目標表中
  insert into test
 select * from test3;
 select * from test order by 1;
 --第八步:提交事務
 commit;
--第十步:分析處理目標表
VACUUM ANALYZE org_whh;
--|ˈvækjuəm|真空; 空白; 空虛; 清潔;
發佈了21 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章