1. 程式人生 > >Hive查詢報錯 Invalid table alias or column reference 'create_time': (possible column names are: _c0, _c1

Hive查詢報錯 Invalid table alias or column reference 'create_time': (possible column names are: _c0, _c1

在執行Hive操作的時候有時候會遇到這種報錯:

 Invalid table alias or column reference 'create_time': (possible column names are: _c0, _c1, _c2, _c3)

下面來分析這個條Hive語句:

select to_date(create_time) as time, count(*) as allNum, count(if(source=3,1,NULL)) as iosNum, count(if(source=4,1,NULL)) as androidNum 
from dg_cook  
where to_date(create_time)>='2016-08-10' and to_date(create_time)<='2016-10-10'  
group by to_date(create_time)   
order by to_date(create_time)
limit 1000

事實上Hive在執行完成group by之後其實已經預設吧以上所有的語句當成了一條,也就是說:

select to_date(create_time) as time, count(*) as allNum, count(if(source=3,1,NULL)) as iosNum, count(if(source=4,1,NULL)) as androidNum 
from dg_cook  
where to_date(create_time)>='2016-08-10' and to_date(create_time)<='2016-10-10'  
group by to_date(create_time)  

到最後執行完成,Hive把它當成一個語句,最後在order by to_date(create_time)時,Hive就不會識別到底啥是to_date(create_time)   

解決方案:

select to_date(create_time) as time, count(*) as allNum, count(if(source=3,1,NULL)) as iosNum, count(if(source=4,1,NULL)) as androidNum 
from dg_cook  
where to_date(create_time)>='2016-08-10' and to_date(create_time)<='2016-10-10'  
group by to_date(create_time)   
order by time  
limit 1000

order by的時候使用別名,就能夠執行過去啦~