GROUP BY Apache Hive中的错误

时间:2017-05-16 16:25:54

标签: sql hadoop group-by hive

我的评分表包含:

userid INT
movieid INT
rating FLOAT
timestmp STRING

select movieid, ROUND(AVG(rating),1) as Rating, COUNT(userid) as rtn_crt, ROUND(((Rating*rtn_cnt)+(100*3.5))/(rtn_cnt+100),1) as w_rating
from ratings 
GROUP BY movieid 
LIMIT 50;

错误消息:

  

org.apache.hive.service.cli.HiveSQLException:编译语句时出错:FAILED:SemanticException [错误10025]:第2行:6表达式不在GROUP BY键rtn_cnt

我尝试使用CAST功能但仍无法正常工作,并收到相同的错误

select movieid, CAST(AVG(rating) AS FLOAT) as Rating, COUNT(userid) as rtn_crt,
CAST((Rating*rtn_cnt) AS FLOAT) + CAST((100*$AVG_MEAN) AS FLOAT)
       /CAST((rtn_cnt+100) AS FLOAT) as w_rating
from ratings 
GROUP BY movieid 
LIMIT 50;

1 个答案:

答案 0 :(得分:0)

我建议你使用子查询重写SQL查询,如下所示:

SELECT t.*, ROUND(((t.Rating*t.rtn_cnt)+(100*3.5))/(t.rtn_cnt+100),1) as w_rating 
FROM (
    SELECT movieid, ROUND(AVG(rating), 1) as Rating, COUNT(userid) as rtn_crt
    FROM ratings 
    GROUP BY movieid 
    LIMIT 50
) t;