How to get other columns with are not in GROUP BY clause in a oracle select sql?

依然范特西╮ 提交于 2019-12-24 02:49:07

问题


I have a table MOVIE containing this data.

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

I need the Top movies data based on SALES_AMT with respect to CATEGORY

The required result is this:

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

If I use GROUP_BY option, I cannot select MOVIE_ID and MOVIE_TITLE

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

回答1:


Try with analytic functions and subquery

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



回答2:


You can use RANK() to achieve this:

SELECT
    MOVIE_ID
    , MOVIE_TITLE
    , CATEGORY
    , SALES_AMT
FROM
    (
        SELECT
            MOVIE_ID
            , MOVIE_TITLE
            , CATEGORY
            , SALES_AMT
            , RANK() OVER (PARTITION BY CATEGORY ORDER BY SALES_AMT DESC) RNK
        FROM MOVIE
    ) Q
WHERE RNK = 1

This would be if you wanted to see duplicates in your results (where sales amounts were equal), otherwise, replace RANK with ROW_NUMBER.




回答3:


Let's say you run this first:

SQL> select deptno, min(sal)
  2  from emp
  3  group by deptno;

and then want to get the employee who has that minimum salary. Then this of course fails:

SQL> select deptno, empno, min(sal)
  2  from emp
  3  group by deptno;

ORA-00979: not a GROUP BY expression

But you can use the KEEP syntax with a window function to assist

SQL> select deptno, min(sal), min(empno) 
  2    KEEP ( dense_rank FIRST order by sal) empno
  3  from emp
  4  group by deptno
  5  /

    DEPTNO   MIN(SAL)      EMPNO
---------- ---------- ----------
        10       1300       7934
        20        800       7369
        30        950       7900

where this is read as "EMPNO 7934 is the person who has the salary of 1300"



来源:https://stackoverflow.com/questions/46974507/how-to-get-other-columns-with-are-not-in-group-by-clause-in-a-oracle-select-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!