2

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)

dnf999
  • 33
  • 2
  • 5
  • If only one sub query is supported, then you will need to separate the queries (there are 3 here, separated by OR) and merge their results. – Dominic Cerisano Dec 09 '16 at 01:14

2 Answers2

0

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
sandeep rawat
  • 4,797
  • 1
  • 18
  • 36
0

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.

leftjoin
  • 36,950
  • 8
  • 57
  • 116