Oracle Row_number函數最常用的使用方法


Row_number函數

語法:row_number() over(parations by order by )

常用於按照某個字段的緯度去篩選掉重復數據,得到唯一的數據

栗子:

with tab_a
as
(
  select  t.empno,
            t.empname,
            row_number() over(paration by t.empno order by t.date desc) rank
     from tab_emp t
   where t.date >= date '2018-01-01'
       and t.date <= date '2018-06-31'
       and t.jobtype = 'saler'

)
select *
   from tab_a a
where a.rank  = 1;

很多表都是設定一個結算的日期,每月結算,這樣就是會導致在一個時間區間內,有的員工可能會出現多次,有的員工可能只會出現一次

使用row_number() over () 函數就可以將出現多次的員工先按照員工的結算月由近到遠排序,分邊賦值為1,2,3,4...

然后只取字段=1的數據,這樣就提取到了員工的最新狀態的數據

是一個很實用的函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM