我需要在同一个表上执行左外连接。因此,我希望表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 :实际上,实际输出是正确的!
答案 0 :(得分:0)
我刚刚在Postgres上运行了你的代码,它返回了四行,这是正确的。 Here是一个SQL小提琴,现在正在使用。
请注意,您的查询可以更轻松地写为:
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);