sql >> データベース >  >> RDS >> Oracle

Oracle SQL-RANK()またはDENSE_RANK()またはROW_NUMBER()分析関数を使用して個別の行を取得する方法は?

    row_numberでより具体的になる場合 、partitioning by dept,salary 次に、row_numberを組み合わせることができます およびdense_rank このクエリのように:

    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. Oracle接続URLのデフォルトスキーマ

    2. 別のテーブルに結合された列として行をリストするテーブルを結合しますか?

    3. 非常に大きなテーブルでの(列ストア)圧縮の楽しみ–パート1

    4. オープン日とクローズ日を指定して、時間の経過に伴うオープンチケットのカウントを生成します