table1(key1 string,key2 string,col1 string,col2 string);
table2(key1 string,key2 string,col1 string,col2 string);
table3(key1 string,key2 string,col1 string,col2 string);
insert overwrite table table1
select t1.key1
,t1.key2
,case when t2.key1 is not null then t2.col1 else t1.col1 end as col1
,case when t2.key1 is not null then t2.col2 else t1.col2 end as col2
from table1 t1
left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2
;
insert overwrite table table1
select t1.key1
,t1.key2
,t1.col1
,t1.col2
from table1 t1
left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2
where t2.key1 is null
;
insert overwrite table table1
select
from(
select t1.key1
,t1.key2
,t1.col1
,t1.col2
from table1 t1
left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2
where t2.key1 is null
union all
select t2.key1
,t2.key2
,t2.col1
,t2.col2
from table2 t2)tt
;
insert overwrite table table1
select
from(
select t1.key1
,t1.key2
,t1.col1
,t1.col2
from table1 t1
left outer join table2 t2 on t1.key1=t2.key1
where t2.key1 is null
union all
select t2.key1
,t2.key2
,t2.col1
,t2.col2
from table2 t2
where t2.udi_type not in ('D'))tt
;