基于条件的连接

isr3a4wc  于 2021-06-28  发布在  Hive
关注(0)|答案(1)|浏览(260)

我有一个很长的配置单元查询,它有10个连接和很多条件,下面是3个条件

1) If id is not equal to XFG or GHT, use field sid
join ABC_Tables on sid  
join CDE_Tables on sid 
2) If id is equal to XFG or GHT, Tested is null, use field pid
join ABC_Tables on kid 
join CDE_Tables on kid
3) If id is equal to XFG or GHT, Tested is not null, use field pid
join ABC_Tables on kid 
join CDE_Tables on kid

我在做什么,

select 1 conditions
union all
select 2 conditions
union all
select 3 conditions

我做得对吗。以上问题的替代方案是什么。

zfycwa2u

zfycwa2u1#

你的条件是允许的 ON 联接条件。配置单元中允许等于/不等于常量 ( ID!='XFG')and(ID!='GHT')and(a.PID=b.PID) 是允许的联接条件。 a.ID not in ('XFG', 'GHT') and a.sid=b.sid 也应该起作用:

select * 
  from a
   left join b on a.ID not in ('XFG', 'GHT') and  a.sid=b.sid
   left join b on a.ID in ('XFG', 'GHT') and Tested is null and  a.pid=b.pid

相关问题