2012-11-20 301 views
0

我試圖獲得數據庫中影片的最低平均評分。SQL嵌套聚合函數

目前我有:

select movie.movietitle as "Lowest Average Rating" 
from movie, rating 
where movie.movieid = rating.movieid 
group by movie.movietitle 
having avg(rating.rating) = min(avg(rating.rating)); 

但我得到的錯誤「組函數嵌套太深。」 任何人都可以提供一個正確的方式來做到這一點,並解釋爲什麼這不起作用?

感謝

回答

1
SELECT MOVIETITLE AS "Movie Title", AVG(RATING) AS "Lowest Average Rating" 
FROM MOVIE, RATING 
WHERE MOVIE.MOVIEID = RATING.MOVIEID 
GROUP BY MOVIETITLE 
HAVING AVG(RATING) = 
(
    SELECT MIN(AVG(RATING)) AS "AVGRating" 
    FROM MOVIE, RATING 
    WHERE MOVIE.MOVIEID = RATING.MOVIEID 
    GROUP BY MOVIETITLE 
) 
+0

噢,對不起,我想我應該改一下。我的意思是「找到電影的平均評級最低的電影標題。」 – Sam

+0

@Sam你看過sqlfiddle上的結果嗎? –

+0

@Sam [SQLFiddle Demo(* click here *)](http://www.sqlfiddle.com/#!4/9a3f2/36) –