Can I do a max(count(*)) in SQL?

前端 未结 11 1812
遥遥无期
遥遥无期 2020-11-29 01:02

Here\'s my code:

    select yr,count(*)  from movie
join casting on casting.movieid=movie.id
join actor on casting.actorid = actor.id
where actor.name = \'Jo         


        
11条回答
  •  情歌与酒
    2020-11-29 02:07

    Use:

      SELECT m.yr, 
             COUNT(*) AS num_movies
        FROM MOVIE m
        JOIN CASTING c ON c.movieid = m.id
        JOIN ACTOR a ON a.id = c.actorid
                    AND a.name = 'John Travolta'
    GROUP BY m.yr
    ORDER BY num_movies DESC, m.yr DESC
    

    Ordering by num_movies DESC will put the highest values at the top of the resultset. If numerous years have the same count, the m.yr will place the most recent year at the top... until the next num_movies value changes.

    Can I use a MAX(COUNT(*)) ?


    No, you can not layer aggregate functions on top of one another in the same SELECT clause. The inner aggregate would have to be performed in a subquery. IE:

    SELECT MAX(y.num)
      FROM (SELECT COUNT(*) AS num
              FROM TABLE x) y
    

提交回复
热议问题