我想从另一个表更新一个表中某些列的数据。
表 cust_tabl
中的这三列 cf_mng,cf_sds,cf_htg
没有数据。
我想更新 cust_tabl
的这三列 cf_mng,cf_sds,cf_htg
的数据
与custom_hist
表的三列cust_cd_cnt_1,cust_cd_cnt_2,cust_cd_cnt_3
的数据。
此表包含从 201505 到 201509 的数据。
CREATE TABLE custom_hist(
cust_no varchar(20),
cust_cd_cnt_1 float,
cust_cd_cnt_2 float,
cust_cd_cnt_3 float,
cust_dt date,
cust_name string)
PARTITIONED BY (yyyymm int);
此表包含从 201403 到 201606 的数据。
CREATE TABLE cust_tabl(
cust_no string,
cf_mng double,
cf_sds double,
cf_htg double,
cust_loc string,
cust_region string,
cust_country string,
cust_reg_id smallint)
PARTITIONED BY (yyyymm int);
请帮助我。
最佳答案
通过主键连接表并覆盖连接的分区。检查主键。连接基数应为 1:1 或 1:0,否则您应该应用一些 row_number
或 rank
或一些聚合(如 max()
)来限制连接后的行:
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.dynamic.partition=true;
insert overwrite table cust_tabl partition (yyyymm)
select
c.cust_no,
coalesce(h.cust_cd_cnt_1,c.cf_mng) as cf_mng, --take history column if joined
coalesce(h.cust_cd_cnt_2,c.cf_sds) as cf_sds, --take original if not joined
coalesce(h.cust_cd_cnt_3,c.cf_htg) as cf_htg,
c.cust_loc, --original columns
c.cust_region,
c.cust_country,
c.cust_reg_id,
c.yyyymm --partition is the last
from cust_tabl c
left join custom_hist h
--assume this is the primary key:
on c.cust_no = h.cust_no and c.yyyymm = h.yyyymm;
关于hive - 如何使用其他表的数据更新 Hive 中表的某些列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40100937/