如何在hive中查询由聚合函数创建的列?

时间:2018-01-02 23:53:25

标签: sql hadoop hive aggregate-functions

在配置单元中,我想选择users>=40的记录。我的表格列由字段userid组成。所以我用了

select title,sum(rating),count(userid) from table_name where count(userid)>=40 group by title order by rating desc

但它显示错误,就像你不能在where子句中使用count。我也试过使用像

这样的别名

select title,sum(rating) as ratings,count(userid) as users where users>=40 group by title order by ratings desc

此处我还遇到错误,显示用户不是表中的列名。 我需要获得最少40位用户的最高评分标题

1 个答案:

答案 0 :(得分:2)

您希望拥有子句:

select title, sum(rating), count(userid) 
rom table_name
group by title
having count(userid) >= 40 
order by sum(rating) desc;

在Hive中,您可能需要使用列别名,但是:

select title, sum(rating) as rating, count(userid) as cnt
rom table_name
group by title
having cnt >= 40 
order by rating desc;