where and or子句错误

qgelzfjb  于 2021-06-28  发布在  Hive
关注(0)|答案(2)|浏览(254)

嗨,我正试图在配置单元中运行此查询,但出现错误10249(不支持的查询表达式-仅支持1个子查询…)

select count(*) from
(
   select * from tableA
   union all
   select * from tableB
) a
where a.field1 in (select fieldA in tableC)
or a.field2 in (select fieldA in tableC)
or a.field3 in (select fieldA in tableC);

有人知道我如何编写这个,以便hive支持这个查询(在sqlserver中运行良好)吗

wxclj1h5

wxclj1h51#

因为您不需要 tableC ,您可以使用 left semi join 而不是 in :

select count(*) from
(
   select * from tableA
   union all
   select * from tableB
) a
  left semi join tableC c1 on a.field1=c1.fieldA 
  left semi join tableC c2 on a.field2=c2.fieldA 
  left semi join tableC c3 on a.field3=c3.fieldA 
;
``` `left semi join` 是半联接,结果集仅包含其中一个联接表的字段,仅返回联接行,类似于内部联接,但如果右表包含多个匹配行,则不会创建重复项。左半连接有效地实现了不相关的in/exists子查询语义。
z4iuyo4d

z4iuyo4d2#

在cte中转换子查询,在where子句中左连接并使用或条件。
例如。

with temp as 
   (
   select * from tableA
   union all
   select * from tableB
   )

select COUNT(a.*)
       from temp a left join tableC a1 on  a.field1 =a1.fieldA
       left join tableC a2 on  a.field2 =a2.fieldA
       left join tableC a3 on  a.field3 =a3.fieldA
     where   a1.fieldA is not null 
          or a3.fieldA is not null
          or a3.fieldA is not null

相关问题