[英]Left outer join for the same table works wrong on redshift
我需要在同一张桌子上执行左外部联接。 结果,我希望所有来自表1的请求列以及仅来自表2的那些连接的列。
复制方法如下:
drop table nz_mri_survey_agg;
create table mri_survey_agg (
HOUSEHOLD_PERSON_HK integer,
MRI_DICTIONARY_ID INTEGER,
rank integer);
insert into mri_survey_agg values (651694412, 2127115057, 36903);
insert into mri_survey_agg values (647638311, 1293574238, 35413);
insert into mri_survey_agg values (647638311, -2076426274, 35413);
insert into mri_survey_agg values (651694412, -2076426274, 35413);
insert into mri_survey_agg values (651694412, -2051582071, 35411);
insert into mri_survey_agg values (647638311, -1747375415, 35613);
insert into mri_survey_agg values (647638311, 1234567, 35610);
这是查询:
select distinct t1.household_person_hk, t2.mri_dictionary_id
from mri_survey_agg t1
left outer join (
select household_person_hk, mri_dictionary_id from mri_survey_agg
where mri_dictionary_id in
(-2076426274, -2051582071, -1747375415)) t2
on t1.household_person_hk = t2.household_person_hk;
我期望下一个输出:
household_person_hk mri_dictionary_id
651694412 -2051582071
647638311 -2076426274
651694412 -2076426274
647638311 -1747375415
647638311 <NaN>
输出为:
household_person_hk mri_dictionary_id
651694412 -2051582071
647638311 -2076426274
651694412 -2076426274
647638311 -1747375415
它在Postgres上可以完美地工作,但是在Redshift上却不能给我预期的结果。
感谢任何提示。
UPD :实际上,实际输出是正确的!
我只是在Postgres上运行了您的代码,它返回了四行,这是正确的。 这是一个SQL Fiddle,现在可以使用。
请注意,您的查询可以更容易地编写为:
select distinct t1.household_person_hk, t2.mri_dictionary_id
from mri_survey_agg t1 left outer join
mri_survey_agg t2
on t1.household_person_hk = t2.household_person_hk and
t2.mri_dictionary_id in (-2076426274, -2051582071, -1747375415);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.