2016-11-04 1 views
1

考えるTABLE1:ハイブの外部結合と非平等な条件

id begin_date end_date  foo 
1 2016-01-01 2016-12-31 1 
1 2017-01-01 2017-12-31 2 

そして表2:

id event_date bar 
1 2016-01-01 100 

は私がしたい:

id begin_date end_date foo bar 
    1 2016-01-01 2016-12-31 1 100 
    1 2017-01-01 2017-01-01 2 NULL 

通常、私は外を行うだけだろう参加:

... 
table1 
left join table2 
on table1.id = table2.id 
    and table2.event_date between table1.begin_date and table1.end_date 

しかし、ハイブは私たちにこれを許可しません。それは私が戻って2017行を取得しないように、内部結合のように働いて終わること

... 
table1 
left join table2 
on table1.id = table2.id 
where 
(table2.event_date is null OR 
table2.event_date between table1.start_date and table1.end_date) 

をしかし:だから私は、where句にダウン日付の比較を移動しようとした、とOR event_date is nullを追加します。私は何か間違っているのですか、それともこれにアプローチする別の方法がありますか?

+0

が参加左 - >外側の左が –

+0

あなたが何を意味するか申し訳ありませんが、確かではないに参加します。 LEFT JOINとLEFT OUTER JOINは同じことです。 – Andrew

+0

あなたは正しいです、私はすぐに答えで何が起こるか説明します –

答えて

0

where句にドロップするため、2017行は表示されません。nullにはevent_dateが含まれず、日付条件もfalseになります。

あなたはbarがnullになりたい場合はあなたの例に続き、 - あなたはselect句に条件を移動する必要があります:

select t1.id, begin_date, end_date, foo, 
    if(table2.event_date is not null and 
     table2.event_date between table1.start_date and table1.end_date, bar, null) from 
table1 left join table2 
on table1.id = table2.id; 
関連する問題