我可以在SQL中执行max(count(*))吗?

时间:2010-03-13 00:55:20

标签: sql

这是我的代码:

    select yr,count(*)  from movie
join casting on casting.movieid=movie.id
join actor on casting.actorid = actor.id
where actor.name = 'John Travolta'
group by yr

这是问题

  

这是'John Travolta'最繁忙的年份。显示他每年制作的电影数量。

这是表结构:

movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)

这是我得到的输出:

yr  count(*)
1976    1
1977    1
1978    1
1981    1
1994    1
etcetc

我需要获取count(*)最大的行

我该怎么做?

11 个答案:

答案 0 :(得分:66)

使用:

  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

num_movies DESC排序会将最高值放在结果集的顶部。如果多年的计数相同,则m.yr会将最近一年放在最顶层...直到下一个num_movies值更改为止。

我可以使用MAX(COUNT(*))吗?


不,您不能在同一个SELECT子句中将聚合函数层叠在一起。内部聚合必须在子查询中执行。 IE:

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

答案 1 :(得分:26)

count(*) desc排序,您将获得最高排名(如果将其与limit 1合并)

答案 2 :(得分:4)

SELECT * from 
(
SELECT yr as YEAR, COUNT(title) as TCOUNT
FROM actor
JOIN casting ON actor.id = casting.actorid
JOIN movie ON casting.movieid = movie.id
WHERE name = 'John Travolta'
GROUP BY yr
order by TCOUNT desc
) res
where rownum < 2

答案 3 :(得分:4)

来自此网站 - http://sqlzoo.net/3.htm 2种可能的解决方案:

TOP 1 a ORDER BY ... DESC:

SELECT yr, COUNT(title) 
FROM actor 
JOIN casting ON actor.id=actorid
JOIN movie ON movie.id=movieid
WHERE name = 'John Travolta'
GROUP BY yr
HAVING count(title)=(SELECT TOP 1 COUNT(title) 
FROM casting 
JOIN movie ON movieid=movie.id 
JOIN actor ON actor.id=actorid
WHERE name='John Travolta'
GROUP BY yr
ORDER BY count(title) desc)
MAX的

SELECT yr, COUNT(title) 
FROM actor  
JOIN casting ON actor.id=actorid    
JOIN movie ON movie.id=movieid
WHERE name = 'John Travolta'
GROUP BY yr
HAVING 
    count(title)=
        (SELECT MAX(A.CNT) 
            FROM (SELECT COUNT(title) AS CNT FROM actor 
                JOIN casting ON actor.id=actorid
                JOIN movie ON movie.id=movieid
                    WHERE name = 'John Travolta'
                    GROUP BY (yr)) AS A)

答案 4 :(得分:3)

将max与限制一起使用只会给你第一行,但如果有两行或更多行具有相同数量的最大电影,那么你将会错过一些数据。如果您有 rank()功能,则可以使用以下方法。

SELECT
    total_final.yr,
    total_final.num_movies
    FROM
    ( SELECT 
        total.yr, 
        total.num_movies, 
        RANK() OVER (ORDER BY num_movies desc) rnk
        FROM (
               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
               WHERE a.name = 'John Travolta'
               GROUP BY m.yr
             ) AS total
    ) AS total_final 
   WHERE rnk = 1

答案 5 :(得分:3)

以下代码为您提供答案。它基本上使用ALL实现MAX(COUNT(*))。它的优点是它使用非常基本的命令和操作。

SELECT yr, COUNT(title)
FROM actor
JOIN casting ON actor.id = casting.actorid
JOIN movie ON casting.movieid = movie.id
WHERE name = 'John Travolta'
GROUP BY yr HAVING COUNT(title) >= ALL
  (SELECT COUNT(title)
   FROM actor
   JOIN casting ON actor.id = casting.actorid
   JOIN movie ON casting.movieid = movie.id
   WHERE name = 'John Travolta'
   GROUP BY yr)

答案 6 :(得分:2)

取决于您正在使用的数据库...

select yr, count(*) num from ...
order by num desc

我的大部分经验都在Sybase中,它使用的语法与其他DB不同。但在这种情况下,您将命名您的计数列,因此您可以按降序对其进行排序。您可以更进一步,将结果限制在前10行(找到他最繁忙的10年)。

答案 7 :(得分:1)

     select top 1 yr,count(*)  from movie
join casting on casting.movieid=movie.id
join actor on casting.actorid = actor.id
where actor.name = 'John Travolta'
group by yr order by 2 desc

答案 8 :(得分:1)

感谢最后的答案

SELECT yr, COUNT(title)
FROM actor
JOIN casting ON actor.id = casting.actorid
JOIN movie ON casting.movieid = movie.id
WHERE name = 'John Travolta'
GROUP BY yr HAVING COUNT(title) >= ALL
  (SELECT COUNT(title)
   FROM actor
   JOIN casting ON actor.id = casting.actorid
   JOIN movie ON casting.movieid = movie.id
   WHERE name = 'John Travolta'
   GROUP BY yr)

我遇到了同样的问题:我只需要知道他们的计数与最大数量匹配的记录(它可能是一个或几个记录)。

我必须更多地了解&#34; ALL子句&#34;,这正是我正在寻找的那种简单的解决方案。

答案 9 :(得分:1)

这个问题很老,但是referenced in a new question on dba.SE。我觉得还没有提供最好的解决方案,所以我又添加了另一个。

首先,假设参照完整性(通常使用外键约束强制执行),您不需要在所有中加入表 2.3.21 。您的查询中已经过期的运费。到目前为止,所有答案都未能指出这一点。

  

我可以在SQL中执行Advertencia: StandardWrapperValve[dispatcher]: Servlet.service() for servlet dispatcher threw exception javax.servlet.ServletException: Could not resolve view with name 'login' in servlet with name 'dispatcher' at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1226) 吗?

要回答标题中的问题:,在Postgres 8.4(2009-07-01发布,在提出此问题之前)或之后,您可以通过将聚合函数嵌套在window function

movie

考虑max(count(*))查询中的事件序列:

(可能的)缺点:窗口函数不聚合行。在聚合步骤之后,您将获得所有行。在某些查询中很有用,但对于这个查询并不理想。

要获得最高计数的 一行 ,您可以SELECT c.yr, count(*) AS ct, max(count(*)) OVER () AS max_ct FROM actor a JOIN casting c ON c.actorid = a.id WHERE a.name = 'John Travolta' GROUP BY c.yr; 使用SELECT

ORDER BY ct LIMIT 1

仅使用任何中途正常RDBMS中提供的基本SQL功能 - SELECT c.yr, count(*) AS ct FROM actor a JOIN casting c ON c.actorid = a.id WHERE a.name = 'John Travolta' GROUP BY c.yr ORDER BY ct DESC LIMIT 1; 实现方式各不相同:

或者您可以使用LIMIT(仅限Postgres)获得最高计数 每行一行

答案

但你要求:

  

... count(*)为max。

的行

可能不止一个。最优雅的解决方案是在子查询中使用Select first row in each GROUP BY group?window function rank()但它可以更简单(我在上面的回答中详述):

DISTINCT ON

现在所有主要的RDBMS支持窗口功能。 MySQL和forks(Ryan provided a query)除外。

答案 10 :(得分:0)

create view sal as
select yr,count(*) as ct from
(select title,yr from movie m, actor a, casting c
where a.name='JOHN'
and a.id=c.actorid
and c.movieid=m.id)group by yr

-----创建视图-----

select yr from sal
where ct =(select max(ct) from sal)

YR 2013