Oracle Analytic function for min value in grouping

前端 未结 4 1461
遥遥无期
遥遥无期 2021-01-12 08:06

I\'m new to working with analytic functions.

DEPT EMP   SALARY
---- ----- ------
  10 MARY  100000
  10 JOHN  200000
  10 SCOTT 300000
  20 BOB   100000
  20 BET         


        
4条回答
  •  难免孤独
    2021-01-12 08:31

    You can use the RANK() syntax. For example, this query will tell you where an employee ranks within their department with regard to how large their salary is:

    SELECT
      dept,
      emp,
      salary,
      (RANK() OVER (PARTITION BY dept ORDER BY salary)) salary_rank_within_dept
    FROM EMPLOYEES
    

    You could then query from this where salary_rank_within_dept = 1:

    SELECT * FROM
      (
        SELECT
          dept,
          emp,
          salary,
          (RANK() OVER (PARTITION BY dept ORDER BY salary)) salary_rank_within_dept
        FROM EMPLOYEES
      )
    WHERE salary_rank_within_dept = 1
    

提交回复
热议问题