0

我必须遵循架构

Movie(mvID, title, rating, year)
Director(directorID, firstname, lastname)
Genre(mvID*, genre)
Direct(mvID*, directorID*)

我需要知道导演最多喜剧类型电影的导演,并输出他们的详细信息以及他们在该类型中制作的电影数量。

所以我有

SELECT Director.DirectorID, Director.FirstName, Director.LastName, COUNT(*)
FROM Direct, Genre, Director
WHERE Direct.mvID = Genre.mvID
AND Genre.genre = 'Comedy'
AND Direct.DirectorID = Director.DirectorID
AND COUNT(*) > ALL
GROUP BY Director.DirectorID, Director.FirstName, Director.LastName

但我得到一个不允许组功能的错误。

4

2 回答 2

1

首先,按导演和类型计算:

SELECT directorID, genre, COUNT(*) num_movies
FROM Movie JOIN Genre
ON Movie.mvID = Genre.mvID
JOIN Direct
ON Movie.mvID = Direct.mvID
GROUP BY directorID, genre

现在,按类型对导演进行排名:

SELECT
   directorID, genre,
   DENSE_RANK() OVER(PARTITION BY genre order by num_movies desc) rank
FROM
(SELECT directorID, genre, COUNT(*) num_movies
FROM Movie JOIN Genre
ON Movie.mvID = Genre.mvID
JOIN Direct
ON Movie.mvID = Direct.mvID
GROUP BY directorID, genre) X

现在,按类型获得顶级导演:

SELECT directorID, genre
FROM (SELECT
   directorID, genre,
   DENSE_RANK() OVER(PARTITION BY genre order by num_movies desc) rank
   FROM
      (SELECT directorID, genre, COUNT(*) num_movies
       FROM Movie JOIN Genre
       ON Movie.mvID = Genre.mvID
       JOIN Direct
       ON Movie.mvID = Direct.mvID
       GROUP BY directorID, genre) )
WHERE rank=1

还有另一种使用 KEEP 和 FIRST 的方法,但我个人觉得 KEEP...FIRST/LAST 语法太混乱了。

于 2013-10-13T12:15:21.897 回答
0

这为您提供了一个顶级列表:

select * from (
SELECT Director.DirectorID, Director.FirstName, Director.LastName, COUNT(*)
FROM Direct, Genre, Director
WHERE Direct.mvID = Genre.mvID
AND Genre.genre = 'Comedy'
AND Direct.DirectorID = Director.DirectorID
GROUP BY Director.DirectorID, Director.FirstName, Director.LastName
ORDER BY COUNT(*) desc
) where rownum<2

(如果这是您想要列出的内容。)

于 2013-10-13T12:04:12.540 回答