Nesting Aggregate Functions - SQL

妖精的绣舞 提交于 2019-11-30 12:00:21

If you are only interested in the value itself, the following should do it:

SELECT MAX(avg_rating)
FROM (
    SELECT AVG(m."Rating") as avg_rating
    FROM awards a, movies m
    WHERE a."Title" = m."Title"
    GROUP BY a."Award"
) t

Otherwise Adrian's solution is better.

This will bring your desired result:

SELECT a."Award", AVG(m."Rating")
FROM awards a, movies m
WHERE a."Title" = m."Title"
GROUP BY a."Award"
ORDER by AVG(m."Rating") desc
LIMIT 1

This will allow you not only get the MAX value, but its corresponding Award info

Did you try this?

SELECT MAX(
   SELECT AVG(m."Rating")
   FROM awards a, movies m
   WHERE a."Title" = m."Title"
   GROUP BY a."Award"
)

Another way is to use windowed MAX:

SELECT MAX(AVG(m."Rating")) OVER()
FROM awards a -- proper JOIN syntax
JOIN movies m ON a."Title" = m."Title"     
GROUP BY a."Award"
LIMIT 1;

db<>fiddle demo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!