在hive表中插入值时出错

时间:2017-08-09 15:04:46

标签: hadoop join hive bigdata

我需要在一个表中插入2个表中的值。 在此表中,我需要为另一个表中的一列中的主要药物和其他药物分配ID。 因此,所有列都来自一个表(d_temp_drugs),只有主要和其他药物的ID将来自其他表(unique_drugs_drug_id)

我正在使用join并获取错误“编译语句时出错:FAILED:SemanticException [错误10007]:_u1-subquery1中的模糊列引用drug_id”

下面是我用来在其中创建表和插入值的代码。请提示修复。

create table study_drug_mapping
(
    trial_identifier string,
    cto_id int,
    primary_drug string,
    primary_drug_id int,
    other_drug string,
    other_drug_id int
)

insert into table study_drug_mapping (trial_identifier, cto_id, primary_drug, primary_drug_id, other_drug, other_drug_id)
select d_temp_drugs.trial_identifier, d_temp_drugs.cto_id, d_temp_drugs.primary_drugs_sorted, unique_drugs_drug_id.drug_id, d_temp_drugs.other_drugs_sorted, unique_drugs_drug_id.drug_id
from
(
select d_temp_drugs.trial_identifier, d_temp_drugs.cto_id, d_temp_drugs.primary_drugs_sorted, drugs.drug_id, d_temp_drugs.other_drugs_sorted, drugs.drug_id
from d_temp_drugs left join unique_drugs_drug_id drugs on d_temp_drugs.primary_drugs_sorted = drugs.unique_drug_name
union
select d_temp_drugs.trial_identifier, d_temp_drugs.cto_id, d_temp_drugs.primary_drugs_sorted, drugs.drug_id, d_temp_drugs.other_drugs_sorted, drugs.drug_id
from d_temp_drugs left join unique_drugs_drug_id drugs on d_temp_drugs.other_drugs_sorted = unique_drugs_drug_id.unique_drug_name
) a

2 个答案:

答案 0 :(得分:0)

试试这个:

create table study_drug_mapping
(
    trial_identifier string,
    cto_id int,
    primary_drug string,
    primary_drug_id int,
    other_drug string,
    other_drug_id int
); 

insert into table study_drug_mapping 
select 
    trial_identifier, 
    cto_id, 
    primary_drugs_sorted, 
    drug_id, 
    other_drugs_sorted, 
    drug_id
from
(
select 
    d_temp_drugs.trial_identifier as trial_identifier, 
    d_temp_drugs.cto_id as cto_id, 
    d_temp_drugs.primary_drugs_sorted as primary_drugs_sorted, 
    drugs.drug_id as drug_id, 
    d_temp_drugs.other_drugs_sorted as other_drugs_sorted, 
    drugs.drug_id as drug_id
from d_temp_drugs left join unique_drugs_drug_id drugs on (d_temp_drugs.primary_drugs_sorted = drugs.unique_drug_name)
union 
select
    d_temp_drugs.trial_identifier as trial_identifier, 
    d_temp_drugs.cto_id as cto_id, 
    d_temp_drugs.primary_drugs_sorted as primary_drugs_sorted, 
    drugs.drug_id as drug_id, 
    d_temp_drugs.other_drugs_sorted as other_drugs_sorted, 
    drugs.drug_id as drug_id
from d_temp_drugs left join unique_drugs_drug_id drugs on (d_temp_drugs.other_drugs_sorted = unique_drugs_drug_id.unique_drug_name)
)a

答案 1 :(得分:0)

请试试这个,你非常接近你引用了最后SUBQUERY的{​​{1}}表,它会引发错误

SELECT