2

您好我正在尝试在 Hive 中运行此查询,但收到错误 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 支持这个查询(在 SQL 服务器中工作正常)

4

2 回答 2

0

在 CTE 中隐藏您的子查询,在 where 子句中左连接和使用或条件。

IE。

   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
于 2016-12-09T07:08:52.213 回答
0

由于您不需要来自 的字段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是半连接,结果集仅包含来自连接表之一的字段,仅返回连接行,类似于内部连接,但如果右表包含多个匹配行,则不会创建重复项。LEFT SEMI JOIN 以一种有效的方式实现了不相关的 IN/EXISTS 子查询语义。

于 2016-12-09T09:16:01.600 回答