【发布时间】:2019-10-17 23:41:03
【问题描述】:
HIVE SQL - 我有 2 个表。 survey 和survey_cmets 结构如下:
create external table if not exists survey(
id string,
category_name string,
subcategory_name string)
STORED AS parquet;
insert into survey(id, category_name, subcategory_name)
values ('1', 'Engine', 'Engine problem other than listed');
insert into survey(id, category_name, subcategory_name)
values ('1', 'Exterior Body', 'Color match of painted parts');
insert into survey(id, category_name, subcategory_name)
values ('1', 'Exterior Body', 'Tail lights');
insert into survey(id, category_name, subcategory_name)
values ('1', 'Heating/Ventilation and Cooling', 'Front windshield fogs up');
insert into survey(id, category_name, subcategory_name)
values ('1', 'Transmission', 'Rough shifting');
create external table if not exists survey_comments(
id string,
category_name_txt string,
subcategory_name_txt string,
comments string)
STORED AS parquet;
insert into survey_comments(id, category_name_txt, subcategory_name_txt)
values ('1', 'Exterior Body', 'Tail lights', 'Moisture in lower portion of rear tail lights along with leaves etc.');
insert into survey_comments(id, category_name_txt, subcategory_name_txt)
values ('1', 'Heating/Ventilation and Cooling', 'Front windshield fogs up', 'Small amount of fog low on front windshield during/after rain.');
insert into survey_comments(id, category_name_txt, subcategory_name_txt)
values ('1', 'Miscellaneous', 'General problem other than listed', 'When filling vehicle with gas; the pumps fill the gas line too quickly, had to hold the pump handle only 1/2 way on.');
insert into survey_comments(id, category_name_txt, subcategory_name_txt)
values ('1', 'Miscellaneous', 'General problem other than listed', 'Touch-up paint too red, not same red as on the car.');
现在我的完整外连接如下所示:
select b.id, b.category_name, b.subcategory_name, a.category_name_txt, a.sub_category_name_txt, a.comments
from survey b full outer join survey_comments a
on (
b.id = a.id and
b.category_name = a.category_name_txt and b.subcategory_name = a.sub_category_name_txt
)
我没有得到survey_comment_txt 中category_name 为“Miscellaneous”的行。我需要在调查和survey_cmets 中不匹配的行作为单独的行和匹配的行。我做错了什么。
【问题讨论】:
-
了解 FULL JOIN ON 返回的内容:INNER JOIN ON rows UNION ALL 不匹配的左右表行,由 NULL 扩展。作为 OUTER JOIN 的一部分,始终知道您想要什么 INNER JOIN。 WHERE 或 INNER JOIN ON 要求在 OUTER JOIN ON 后从表中删除由 NULL 扩展的任何行,即只保留 LEFT/RIGHT/INNER JOIN ON 行,因此右/左/两个表列不为 NULL,即“将外部联接变为内部联接”。你有那个。
标签: hive hiveql full-outer-join