簡體   English   中英

相同表的左外部聯接在redshift上工作錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM