Hive Exists 条件不适用于多种条件

Posted

技术标签:

【中文标题】Hive Exists 条件不适用于多种条件【英文标题】:Hive Exists condition not working on multiple condition 【发布时间】:2020-02-19 09:39:14 【问题描述】:

我正在运行以下查询

select * from Data t1 
where not exists (select 1 from data2 t2 where
t1.diagonosiscode2 = t2.dx and 
t1.diagonosiscode3 = t2.dx and
t1.diagonosiscode4 = t2.dx and
t1.diagonosiscode5 = t2.dx and
t1.diagonosiscode6 = t2.dx and
t1.diagonosiscode7 = t2.dx and
t1.diagonosiscode8 = t2.dx and
t1.diagonosiscode9 = t2.dx and
t1.diagonosiscode10 = t2.dx and
t1.diagonosiscode11 = t2.dx and
t1.diagonosiscode12 = t2.dx and
t1.diagonosiscode13 = t2.dx and
t1.diagonosiscode14 = t2.dx and
t1.diagonosiscode15 = t2.dx and
t1.diagonosiscode16 = t2.dx and
t1.diagonosiscode17 = t2.dx and
t1.diagonosiscode18 = t2.dx and
t1.diagonosiscode19 = t2.dx and
t1.diagonosiscode20 = t2.dx and
t1.diagonosiscode21 = t2.dx and
t1.diagonosiscode22 = t2.dx and
t1.diagonosiscode23 = t2.dx and
t1.diagonosiscode24 = t2.dx and
t1.diagonosiscode25 = t2.dx )

上述查询引发以下错误。

org.apache.hive.service.cli.HiveSQLException:编译时出错 语句:失败:SemanticException [错误 10250]:第 2:33 行无效 子查询表达式 'dx':对于 Exists/Not Exists 运算符,子查询必须 是相关的。

但是当我为单个条件编写时执行相同的查询

select * from Data t1 
where not exists (select 1 from  Data2 t2
                  where t1.diagnosiscode3 = t2.dx)

hive 是否只支持一个存在条件。

由于 hive 不支持,我也无法在条件下执行多个。

如何解决问题。

【问题讨论】:

【参考方案1】:

尝试使用left join

select t1.*
from Data t1 left join
     data2 t2 
     on t1.diagonosiscode2 = t2.dx and 
        t1.diagonosiscode3 = t2.dx and
        . . .
where t2.dx is null;

我注意到您正在将所有代码与dx 进行比较。所以你也可以这样做:

select t1.*
from Data t1 
where t1.diagonosiscode2 = t1.diagonosiscode3 and 
      t1.diagonosiscode2 = t1.diagonosiscode4 and
      . . .
     not exists (select 1
                 from data2 t2
                 where t1.diagonosiscode4 = t2.dx 
                );

【讨论】:

【参考方案2】:

也是这个条件

where
t1.diagonosiscode2 = t2.dx and 
t1.diagonosiscode3 = t2.dx and
t1.diagonosiscode4 = t2.dx and
t1.diagonosiscode5 = t2.dx and
t1.diagonosiscode6 = t2.dx and
t1.diagonosiscode7 = t2.dx and
t1.diagonosiscode8 = t2.dx and
t1.diagonosiscode9 = t2.dx and
t1.diagonosiscode10 = t2.dx and
t1.diagonosiscode11 = t2.dx and
t1.diagonosiscode12 = t2.dx and
t1.diagonosiscode13 = t2.dx and
t1.diagonosiscode14 = t2.dx and
t1.diagonosiscode15 = t2.dx and
t1.diagonosiscode16 = t2.dx and
t1.diagonosiscode17 = t2.dx and
t1.diagonosiscode18 = t2.dx and
t1.diagonosiscode19 = t2.dx and
t1.diagonosiscode20 = t2.dx and
t1.diagonosiscode21 = t2.dx and
t1.diagonosiscode22 = t2.dx and
t1.diagonosiscode23 = t2.dx and
t1.diagonosiscode24 = t2.dx and
t1.diagonosiscode25 = t2.dx

可以转化为这个条件:

where
t1.diagonosiscode2 = t2.dx and 
t1.diagonosiscode3 = t1.diagonosiscode2 and
t1.diagonosiscode4 = t1.diagonosiscode2 and
t1.diagonosiscode5 = t1.diagonosiscode2 and
t1.diagonosiscode6 = t1.diagonosiscode2 and
t1.diagonosiscode7 = t1.diagonosiscode2 and
t1.diagonosiscode8 = t1.diagonosiscode2 and
t1.diagonosiscode9 = t1.diagonosiscode2 and
t1.diagonosiscode10 = t1.diagonosiscode2 and
t1.diagonosiscode11 = t1.diagonosiscode2 and
t1.diagonosiscode12 = t1.diagonosiscode2 and
t1.diagonosiscode13 = t1.diagonosiscode2 and
t1.diagonosiscode14 = t1.diagonosiscode2 and
t1.diagonosiscode15 = t1.diagonosiscode2 and
t1.diagonosiscode16 = t1.diagonosiscode2 and
t1.diagonosiscode17 = t1.diagonosiscode2 and
t1.diagonosiscode18 = t1.diagonosiscode2 and
t1.diagonosiscode19 = t1.diagonosiscode2 and
t1.diagonosiscode20 = t1.diagonosiscode2 and
t1.diagonosiscode21 = t1.diagonosiscode2 and
t1.diagonosiscode22 = t1.diagonosiscode2 and
t1.diagonosiscode23 = t1.diagonosiscode2 and
t1.diagonosiscode24 = t1.diagonosiscode2 and
t1.diagonosiscode25 = t1.diagonosiscode2

【讨论】:

以上是关于Hive Exists 条件不适用于多种条件的主要内容,如果未能解决你的问题,请参考以下文章

MS Access:WHERE-EXISTS-clause 不适用于视图?

条件不适用于 DragEnd 事件

NotNull 条件不适用于 spark 数据框 scala 中的 withColumn 条件

Azure 条件访问不适用于令牌获取请求?

为啥三元条件不适用于字符串连接

为啥条件渲染不适用于 vuejs 中的表单输入