按子查询或联接的配置单元sql avg表

j9per5c4  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(316)

我有两张table:
电影

movieid int                                         
title   string

评级

userid    int                                       
movieid   int                                       
rating    float                                     
timestamp string

我想得到这样的结果

movietitle ratingavg
--------------------   
movie 1     4.8
movie 2      8

我的问题是:

select avg(r.rating), m.title 
from ratings r 
join movies m on r.movieId = m.movieId;

还有这个

select m.title, (select avg(r.rating) from ratings r) 
from movies m 
join ratings r on r.movieId = m.movieId;

但我得到一个错误:
失败:semanticexception[错误10025]:行1:21表达式不在group by键“title”中

1cosmwyk

1cosmwyk1#

SELECT avg(r.rating)
    ,m.title
FROM ratings r
JOIN movies m ON r.movieId = m.movieId
GROUP BY m.title;

相关问题