Hive 完全外部连接行为

Hive 完全外部连接行为,hive,hiveql,full-outer-join,Hive,Hiveql,Full Outer Join,配置单元SQL-我有两个表。调查和调查意见,结构如下所示: 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 o

配置单元SQL-我有两个表。调查和调查意见,结构如下所示:

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
)
现在,我的完全外部联接如下所示:

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中获得类别名称为“Miscellaneous”的行。我需要在survey和survey_注释中使用不匹配的行,以及单独的行和匹配的行。我做错了什么。

使用CTEs进行测试,发现了两个问题1st:您在调查注释中插入了四列,但只指定了三列2nd:查询列中的名称应为子类别名称\U txt,而不是子类别名称\U txt

固定后的测试:

with survey as (
select stack(5,
'1', 'Engine', 'Engine problem other than listed',
'1', 'Exterior Body', 'Color match of painted parts',
'1', 'Exterior Body', 'Tail lights',
'1', 'Heating/Ventilation and Cooling', 'Front windshield fogs up',
'1', 'Transmission', 'Rough shifting') as (id, category_name, subcategory_name) 
),

survey_comments as (
select stack(4,

'1', 'Exterior Body', 'Tail lights', 'Moisture in lower portion of rear tail lights along with leaves etc.',
'1', 'Heating/Ventilation and Cooling', 'Front windshield fogs up', 'Small amount of fog low on front windshield during/after rain.',
'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.',
'1', 'Miscellaneous', 'General problem other than listed', 'Touch-up paint too red, not same red as on the car.') as (id, category_name_txt, subcategory_name_txt,comments)
)

select b.id, b.category_name, b.subcategory_name, a.category_name_txt, a.subcategory_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.subcategory_name_txt
)
返回:

b.id    b.category_name b.subcategory_name  a.category_name_txt a.subcategory_name_txt  a.comments  
1   Engine  Engine problem other than listed    NULL    NULL    NULL    
1   Exterior Body   Color match of painted parts    NULL    NULL    NULL    
1   Exterior Body   Tail lights Exterior Body   Tail lights Moisture in lower portion of rear tail lights along with leaves etc.    
1   Heating/Ventilation and Cooling Front windshield fogs up    Heating/Ventilation and Cooling Front windshield fogs up    Small amount of fog low on front windshield during/after rain.  
NULL    NULL    NULL    Miscellaneous   General problem other than listed   Touch-up paint too red, not same red as on the car. 
NULL    NULL    NULL    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.    
1   Transmission    Rough shifting  NULL    NULL    NULL    

返回杂项。

使用CTE测试发现两个问题1st:您在调查注释中插入了四列,但只指定了三列2nd:查询列中的名称应为子类别名称\U txt,而不是子类别名称\U txt

固定后的测试:

with survey as (
select stack(5,
'1', 'Engine', 'Engine problem other than listed',
'1', 'Exterior Body', 'Color match of painted parts',
'1', 'Exterior Body', 'Tail lights',
'1', 'Heating/Ventilation and Cooling', 'Front windshield fogs up',
'1', 'Transmission', 'Rough shifting') as (id, category_name, subcategory_name) 
),

survey_comments as (
select stack(4,

'1', 'Exterior Body', 'Tail lights', 'Moisture in lower portion of rear tail lights along with leaves etc.',
'1', 'Heating/Ventilation and Cooling', 'Front windshield fogs up', 'Small amount of fog low on front windshield during/after rain.',
'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.',
'1', 'Miscellaneous', 'General problem other than listed', 'Touch-up paint too red, not same red as on the car.') as (id, category_name_txt, subcategory_name_txt,comments)
)

select b.id, b.category_name, b.subcategory_name, a.category_name_txt, a.subcategory_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.subcategory_name_txt
)
返回:

b.id    b.category_name b.subcategory_name  a.category_name_txt a.subcategory_name_txt  a.comments  
1   Engine  Engine problem other than listed    NULL    NULL    NULL    
1   Exterior Body   Color match of painted parts    NULL    NULL    NULL    
1   Exterior Body   Tail lights Exterior Body   Tail lights Moisture in lower portion of rear tail lights along with leaves etc.    
1   Heating/Ventilation and Cooling Front windshield fogs up    Heating/Ventilation and Cooling Front windshield fogs up    Small amount of fog low on front windshield during/after rain.  
NULL    NULL    NULL    Miscellaneous   General problem other than listed   Touch-up paint too red, not same red as on the car. 
NULL    NULL    NULL    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.    
1   Transmission    Rough shifting  NULL    NULL    NULL    

Miscellaneous
返回。

了解返回时的完全联接:行上的内部联接将所有不匹配的左、右表行合并为null扩展。始终知道作为外部联接的一部分,您需要什么样的内部联接。上的WHERE或内部联接,在上的外部联接从表中删除由NULL扩展的任何行后,要求右/左/双表列不为NULL,即只在行上保留左/右/内部联接,即“将外部联接转换为内部联接”。您已经知道了。可能重复了“了解返回时的完全联接:行上的内部联接”并将所有不匹配的左表行和右表行扩展为null。始终知道作为外部联接的一部分,您需要什么样的内部联接。上的WHERE或内部联接,在上的外部联接从表中删除由NULL扩展的任何行后,要求右/左/双表列不为NULL,即只在行上保留左/右/内部联接,即“将外部联接转换为内部联接”。你有,可能是你的复制品