2

I am trying to run a union all query in hive

select * from tabName where col1='val1' and col2 = 'val2' limit 10 union all select * from tabName where col1='val1' and col2 = 'val3' limit 10;

but i get

FAILED: ParseException line 1:105 missing EOF at 'union' near '10'

I also tried

( select * from tabName where col1='val1' and col2 = 'val2' limit 10 ) as a union all ( select * from tabName where col1='val1' and col2 = 'val3' limit 10 ) as b;

but i got

FAILED: ParseException line 1:109 missing EOF at 'as' near ')'

what am i doing wrong ?

leftjoin
  • 36,950
  • 8
  • 57
  • 116
AbtPst
  • 7,778
  • 17
  • 91
  • 172

2 Answers2

6

Use select from subquery:

select * from
( select * from tabName where col1='val1' and col2 = 'val2' limit 10 ) a 
union all 
select * from
( select * from tabName where col1='val1' and col2 = 'val3' limit 10 ) b;
leftjoin
  • 36,950
  • 8
  • 57
  • 116
0

I offer another way using with clause:

with query1 as (
  select *
  from tabName
  where col1 = 'val1' and col2 = 'val2'
  limit 10
),
query2 as (
  select *
  from tabName
  where col1 = 'val1' and col3 = 'val3'
  limit 10
)
select * from query1
union all
select * from query2
Lizhi Liu
  • 97
  • 9