Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive Union Group By Error

In the following query, I am trying to count up the distinct, and total occurences of the column "primary" and and summarise this for each column.

I have two source tables, which contain similar information. I want to union these to pull all the information together before counting this up.

However, using the logic below I get the following error. Can someone please advise where this is going wrong?

select COUNT(distinct primary), COUNT(primary), mycolumn 
from  (select primary, mycolumn from mytablea where mycolumn >= a and mycolumn <= b 
     union all 
     select primary, mycolumn from mytableb where mycolumn >= a and mycolumn <= b) 
group by mycolumn

FAILED: ParseException missing EOF at 'by' near 'group'

Thanks..

like image 598
Tminer Avatar asked Nov 06 '14 12:11

Tminer


1 Answers

You have to give an alias to the subquery. Something like this should work:

select COUNT(distinct primary), COUNT(primary), mycolumn 
from  (select primary, mycolumn from mytablea where mycolumn >= a and mycolumn <= b 
     union all 
     select primary, mycolumn from mytableb where mycolumn >= a and mycolumn <= b) q1
group by mycolumn
like image 170
Amar Avatar answered Nov 24 '22 22:11

Amar