16

I have table_A:

id var1 var2
1   a    b
2   c    d

Table_B:

id var1 var2  
3   e    f
4   g    h

All I want is table, combined:

id var1 var2
1   a    b
2   c    d
3   e    f
4   g    h 

This is my .hql:

CREATE TABLE combined AS
SELECT all.id, all.var1, all.var2
FROM (
  SELECT a.id, a.var1, a.var2  
  FROM table_A a  
  UNION ALL  
  SELECT b.id, b.var1, b.var2  
  FROM table_B b
) all;

I'm coding straight from page 112 of Programming Hive by Edward Capriolo, et al.

The error I get, no matter what ostensibly reasonable variation of the above that I try, is

cannot recognize input near '.' 'id' ',' in select expression.

I have tried using AS between the table name and the alias, asterisks since I want everything from both tables. Same error. I've tried other things and gotten other errors... All I want to do is UNION two tables. (I've tried UNION instead of UNION ALL — same error).

Artyom Ionash
  • 405
  • 7
  • 17
dum_dum_dummy
  • 161
  • 1
  • 1
  • 3

3 Answers3

15

Just replace all with another word. It seems to be a reserved keyword. E.g:

CREATE TABLE combined AS
SELECT unioned.id, unioned.var1, unioned.var2
FROM (
  SELECT a.id, a.var1, a.var2  
  FROM table_A a  
  UNION ALL  
  SELECT b.id, b.var1, b.var2  
  from table_B b
) unioned;
Lorand Bendig
  • 10,630
  • 1
  • 38
  • 45
2

I have similar query working. Just changing table name and column names. Try this. Hope this helps you.

create table new_table as
select
distinct
id, name
FROM
table1
union all
select
distinct
id,name
from
table2 
;
kalpesh
  • 328
  • 3
  • 14
1

Try this, it worked for me.

CREATE TABLE combined AS
SELECT id, var1, var2
FROM (
  SELECT id, var1, var2  
  FROM table_A
  UNION ALL  
  SELECT id, var1, var2  
  from table_B
) a;
Andronicus
  • 25,419
  • 17
  • 47
  • 88