[英]Update column in hive table based on another table
我有一種情況,我需要根據另一張表中的某些條件來更新列的值。 數據如下:
ID Date Amount
00 02/01 0
00 02/01 0
01 05/01 100
01 05/01 0
另一個表包含以下內容:
ID Date Amount
00 02/01 0
00 02/01 0
我需要更改第二張表中的日期列,以匹配第一張表中ID'01'的日期值。 我嘗試了加入的選項,但它似乎無法正常工作。 什么是最簡單的解決方案?
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大小寫
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
希望這能解決您的問題。
您可以創建一個新表,然后再刪除舊表,因為除非表已設置事務屬性,否則無法更新表。
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.