繁体   English   中英

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

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

我的 SQL 查询有问题。 我有以下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

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

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

我想要每个性别的最大值的标题。 谁能告诉我我的错误在哪里? 提前谢谢你!

我怀疑您的查询实际上并未聚合,因为您希望(genre, title)元组在表中是唯一的。

您可以使用相关子查询来过滤表:

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

在我看来,这似乎是表达您要实现的目标的最简单方法。 对于性能,请考虑(genre, budget)上的索引。

这是一个关于 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

您可以使用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

分组是通过分区来执行的,预算的最大值是通过按预算降序来找到的。

演示

在 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;

keep表达式本质上是在执行获取第一个值 function。

暂无
暂无

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

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