Oracle Row

it2022-05-08  12

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的数据,这样就提取到了员工的最新状态的数据

是一个很实用的函数

转载于:https://www.cnblogs.com/MoreDrinkHotWater/p/9791512.html


最新回复(0)