简体   繁体   English

SQL 使用 GROUP BY 查询多列和聚合 function

[英]SQL Query with multiple columns and an aggregate function using GROUP BY

I am having troubles with my SQL query.我的 SQL 查询有问题。 I have the following movie table:我有以下movie表:

 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID_MOVIE                                  NOT NULL NUMBER(4)
 TITLE                                     NOT NULL VARCHAR2(30)
 GENRE                                     NOT NULL VARCHAR2(30)
 YEAR                                      NOT NULL NUMBER(4)
 COUNTRY                                   NOT NULL VARCHAR2(30)
 DURATION                                  NOT NULL NUMBER(3)
 BUDGET                                             NUMBER(10)
 INCOMES                                            NUMBER(10)
 ID_MOVIE_PREV                                      NUMBER(4)
 ID_DIRECTOR

i tried this command: select m.genre, max(m.budget),m.title from movie m group by m.genre,m.title;我试过这个命令: select m.genre, max(m.budget),m.title from movie m group by m.genre,m.title; and got this result:并得到了这个结果:

GENRE                          MAX(M.BUDGET) TITLE
------------------------------ ------------- ------------------------------
Western                              1200000 The Good, the Bad and the Ugly
Horror                                806947 Psycho
Crime                                7000000 The Godfather: Part III
Action                             185000000 The Dark Knight
Drama                               26000000 Philadelphia
Drama                               13000000 In the Name of the Father
Action                             150000000 Batman Begins
Historical                          23800000 The Last Emperor
Science-fiction                      5800000 Planet of the Apes
Crime                                7000000 The Godfather
Action                             230000000 The Dark Knight Rises

GENRE                          MAX(M.BUDGET) TITLE
------------------------------ ------------- ------------------------------
Comedy                              28000000 Zoolander
Crime                                9000000 Pulp Fiction
Crime                               13000000 The Godfather: Part II
War                                 70000000 Saving Private Ryan
Science-fiction                     28000000 Blader Runner
Drama                               33000000 Gran Torino

I'd like to have the title of the maximum of each gender.我想要每个性别的最大值的标题。 Can anyone tell me on where is my mistake?谁能告诉我我的错误在哪里? Thank you by advance!提前谢谢你!

I suspect that your query does not actually aggregate, since you would expect that (genre, title) tuples are unique in the table.我怀疑您的查询实际上并未聚合,因为您希望(genre, title)元组在表中是唯一的。

You can use a correlated subquery to filter the table:您可以使用相关子查询来过滤表:

select genre,title, budget
from movie m
where budget = (
    select max(m1.budget)
    from movie m1
    where m1.genre = m.genre
)

This seems to me like the simplest way to phrase what you are trying to achieve.在我看来,这似乎是表达您要实现的目标的最简单方法。 For performance, consider an index on (genre, budget) .对于性能,请考虑(genre, budget)上的索引。

Here is a demo on DB Fiddle (based on the above assumption that your current resultset can be considered as your raw data):这是一个关于 DB Fiddle 的演示(基于上述假设,您当前的结果集可以被视为您的原始数据):

GENRE           | TITLE                          |    BUDGET
:-------------- | :----------------------------- | --------:
Western         | The Good, the Bad and the Ugly |   1200000
Horror          | Psycho                         |    806947
Historical      | The Last Emperor               |  23800000
Action          | The Dark Knight Rises          | 230000000
Comedy          | Zoolander                      |  28000000
Crime           | The Godfather: Part II         |  13000000
War             | Saving Private Ryan            |  70000000
Science-fiction | Blader Runner                  |  28000000
Drama           | Gran Torino                    |  33000000

You can use row_number() analytic function:您可以使用row_number()解析 function:

select genre, budget,title
  from
  (
      select genre, budget, title,
             row_number() over (partition by genre order by budget desc) as rn       
        from movie 
   )
  where rn = 1

grouping is performed by partition by and maximum value for budget is found by ordering by descending budget.分组是通过分区来执行的,预算的最大值是通过按预算降序来找到的。

Demo 演示

In Oracle, you can use aggregation:在 Oracle 中,可以使用聚合:

select genre,
       max(title) keep (dense_rank first order by budget desc) as title,
       max(budget) as budget
from movie m
group by genre;

The keep expression is essentially doing a get-the-first-value function. keep表达式本质上是在执行获取第一个值 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM