一尘不染

如何在oracle select sql中的GROUP BY子句中获取不在列中的其他列?

sql

我有一个包含该数据的表MOVIE。

MOVIE_ID  MOVIE_TITLE              CATEGORY        SALES_AMT
--------- ------------------------ --------------- ----------
M_0000004 The Boss Baby            Animation       2000
M_0000006 Star Wars: The Last Jedi Science Fiction 3000
M_0000007 Get Out                  Horror          4000
M_0000008 Million Dollar Arm       Action          2000
M_0000009 The Conjuring            Horror          1000
M_0000012 The Dark Knight          Action          3000

我需要根据热门电影数据SALES_AMT相对于CATEGORY

所需的结果是这样的:

MOVIE_ID  MOVIE_TITLE              CATEGORY        SALES_AMT
--------- ------------------------ --------------- ----------
M_0000004 The Boss Baby            Animation       2000
M_0000006 Star Wars: The Last Jedi Science Fiction 3000
M_0000007 Get Out                  Horror          4000
M_0000012 The Dark Knight          Action          3000

如果使用GROUP_BY选项,则无法选择MOVIE_IDMOVIE_TITLE

select CATEGORY, MAX(SALES_AMT)
from MOVIE
group by CATEGORY
;

阅读 167

收藏
2021-03-08

共1个答案

一尘不染

尝试使用解析函数和子查询

select movie_id, movie_title, category, sales_amt 
from (
  select movie_id, movie_title, category, sales_amt, 
  row_number() over (partition by category order by sales_amt desc) r 
  from movie
) where r = 1
2021-03-08