[英]Left outer join for the same table works wrong on redshift
I need to perform left outer join on the same table. 我需要在同一张桌子上执行左外部联接。 As a result I expect all requested columns from table 1 and only those from table 2 which joined. 结果,我希望所有来自表1的请求列以及仅来自表2的那些连接的列。
Here is how to reproduce: 复制方法如下:
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);
Here is the query: 这是查询:
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;
I expect the next output: 我期望下一个输出:
household_person_hk mri_dictionary_id
651694412 -2051582071
647638311 -2076426274
651694412 -2076426274
647638311 -1747375415
647638311 <NaN>
The output is: 输出为:
household_person_hk mri_dictionary_id
651694412 -2051582071
647638311 -2076426274
651694412 -2076426274
647638311 -1747375415
It works perfect on Postgres, but doesn't give me expected results on Redshift. 它在Postgres上可以完美地工作,但是在Redshift上却不能给我预期的结果。
Appreciate for any hints. 感谢任何提示。
UPD : Actually, the actual output is correct! UPD :实际上,实际输出是正确的!
I just ran your code on Postgres and it returns four rows, which is correct. 我只是在Postgres上运行了您的代码,它返回了四行,这是正确的。 Here is a SQL Fiddle, which is now working. 这是一个SQL Fiddle,现在可以使用。
Note that your query can more easily be written as: 请注意,您的查询可以更容易地编写为:
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.