问题
Hi I am trying to run this query in Hive, but get the error 10249 (Unsupported query expression - only 1 subquery is supported...)
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);
Would anybody know how I can write this so that Hive supports this query (works fine in SQL server)
回答1:
Since you do not need fields from tableC
, you can use left semi join
instead of 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
is half join, the result set contains fields only from one of joined tables, only joined rows returned, similar to inner join but will not create duplicates if right table contains multiple matching rows.
LEFT SEMI JOIN implements the uncorrelated IN/EXISTS subquery semantics in an efficient way.
回答2:
Covert you sub query in CTE , left join and use or condition in where clause.
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
来源:https://stackoverflow.com/questions/41050097/hive-where-and-or-clause-error