根据另一个 table 更新配置单元 table 中的列
Update column in hive table based on another table
我有一种情况需要根据另一个 table 的某些条件更新列的值。数据如下:
ID Date Amount
00 02/01 0
00 02/01 0
01 05/01 100
01 05/01 0
另一个 table 包含以下内容:
ID Date Amount
00 02/01 0
00 02/01 0
我需要更改第二个 table 中的日期列以匹配第一个 table 中 ID“01”的日期值。我尝试了加入它的选项,但它似乎无法正常工作。最简单的解决方案是什么?
您可以创建一个新的 table 然后删除旧的,因为更新 table 是不可能的,除非 table 设置了交易属性。
create new_table2
location 'HDFS path' as
select t2.id,d.date,t2.amount
from table2 t2
cross join (select max(date) as date from table1 where id='01') d;
/*This assumes there is one distinct date for id=01 in table1*/
drop table table2 purge;
insert overwrite table table2
select t1.id,
t2.Date,
t2.amount
from table2 t2 left join table t1
on t1.id=t2.id
如果表 1 中缺少 ID 的值为空值,则可以包含 when case
insert overwrite table table2
select case when(t1.id is null) then 0 else t1.id end,
t2.Date,
t2.amount
from table2 t2 left join table t1
on t1.id=t2.id
希望这能解决您的问题。
我有一种情况需要根据另一个 table 的某些条件更新列的值。数据如下:
ID Date Amount
00 02/01 0
00 02/01 0
01 05/01 100
01 05/01 0
另一个 table 包含以下内容:
ID Date Amount
00 02/01 0
00 02/01 0
我需要更改第二个 table 中的日期列以匹配第一个 table 中 ID“01”的日期值。我尝试了加入它的选项,但它似乎无法正常工作。最简单的解决方案是什么?
您可以创建一个新的 table 然后删除旧的,因为更新 table 是不可能的,除非 table 设置了交易属性。
create new_table2
location 'HDFS path' as
select t2.id,d.date,t2.amount
from table2 t2
cross join (select max(date) as date from table1 where id='01') d;
/*This assumes there is one distinct date for id=01 in table1*/
drop table table2 purge;
insert overwrite table table2
select t1.id,
t2.Date,
t2.amount
from table2 t2 left join table t1
on t1.id=t2.id
如果表 1 中缺少 ID 的值为空值,则可以包含 when case
insert overwrite table table2
select case when(t1.id is null) then 0 else t1.id end,
t2.Date,
t2.amount
from table2 t2 left join table t1
on t1.id=t2.id
希望这能解决您的问题。