Oracle
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Oracle

Oracle SQL - Làm thế nào để có được các hàng riêng biệt bằng cách sử dụng hàm phân tích RANK () hoặc DENSE_RANK () hoặc ROW_NUMBER ()?

Nếu bạn hiểu cụ thể hơn trong row_number , với partitioning by dept,salary thì bạn có thể kết hợp row_numberdense_rank như trong truy vấn này:

with data_row as 
( 
select 7839 as empno, 10 as deptno, 5000 as salary from dual union all
select 7782 as empno, 10 as deptno, 2450 as salary from dual union all
select 7934 as empno, 10 as deptno, 1300 as salary from dual union all
select 1111 as empno, 10 as deptno, 1111 as salary from dual union all
select 7935 as empno, 20 as deptno, 6000 as salary from dual union all
select 7788 as empno, 20 as deptno, 3000 as salary from dual union all
select 7902 as empno, 20 as deptno, 3000 as salary from dual union all
select 7566 as empno, 20 as deptno, 2975 as salary from dual union all
select 2222 as empno, 20 as deptno, 2222 as salary from dual union all
select 7698 as empno, 30 as deptno, 2850 as salary from dual union all
select 7499 as empno, 30 as deptno, 1600 as salary from dual union all
select 7844 as empno, 30 as deptno, 1500 as salary from dual union all
select 3333 as empno, 30 as deptno, 1333 as salary from dual
)
select *
from
(
select 
       deptno,
       salary,
       dense_rank() over (partition by deptno order by salary desc) as drank,
       row_number() over (partition by deptno, salary order by salary desc) as rowno             

from data_row
)
where drank <=3 and
      rowno =1


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Sự cố NLS_CHARACTERSET WE8ISO8859P1 và UTF8 trong Oracle

  2. Sử dụng GUID của Oracle () - ID được tạo trong Grails / Hibernate

  3. Hàm NVL2 () trong Oracle

  4. Cập nhật nhiều cột trong câu lệnh MERGE ORACLE

  5. Tại sao tôi nhận được Ngoại lệ OutOfRange trong Hàm GetOrdinal của trường CLOB này?