[英]GROUP BY on one column with multiple columns in SELECT
I have a relation of movies with the following attributes: movie_id, title, director, genres, year, revenue_generated, short_description, runtime, and rating.我有一个具有以下属性的电影关系:movie_id、title、director、genres、year、revenue_generated、short_description、runtime 和 rating。
I want to find the movies from each genre that have the highest revenue.我想找到收入最高的每种类型的电影。 I would like to return the movie_id, title, genre, and revenue of the movie from each genre that had the highest revenue.我想返回电影的 movie_id、标题、流派和收入最高的每种类型的电影的收入。
I've tried to the following query:我试过以下查询:
SELECT movie_id, title, genres, MAX(revenue_generated) AS highest_revenue
FROM movies
GROUP BY genres;
But get the following error:但是得到以下错误:
ERROR: column "movies.movie_id" must appear in the GROUP BY clause or be used in an aggregate function
I know this isn't working because it doesn't know how to "group" the movie_id and title by each category.我知道这是行不通的,因为它不知道如何按每个类别对 movie_id 和标题进行“分组”。 If I were to do the following query:如果我要执行以下查询:
SELECT genres, MAX(revenue_generated) AS highest_revenue
FROM movies
GROUP BY genres;
how would I also get the movie_id and title for these observations in the output?我如何在 output 中获得这些观察结果的 movie_id 和标题? Here is a screenshot of some of the data being used:这是正在使用的一些数据的屏幕截图:
I want to find the movies from each genre that have the highest revenue我想找到收入最高的每种类型的电影
In SQL Server, and several other platforms, you would use ROW_NUMBER() here, eg:在 SQL 服务器和其他几个平台中,您将在此处使用 ROW_NUMBER(),例如:
with q as
(
SELECT movie_id,
title,
genre,
revenue_generated, row_number() over (partition by genre order by revenue_generated desc) rn
FROM movies
)
select movie_id,
title,
genre,
revenue_generated
from q
where rn = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.