关于Oracle MAX()KEEP(DENSE_RANK LAST/FIRST ORDER BY ) 函数的使用分析
max()/min() keep(dense_rank last/first) 函数
解释:
1. max() 获取最大值;
2.min() 获取最小值;
3. keep 保持满足括号内条件的内容;
4.dense_rank 排序策略,连续排序,如果有两个同一级别时,接下来是第二级别 ,例如1,2,2,3
select names,dept,dense_rank() over(partition by dept order by age desc) rank from workers;
结果如下图
5.rank 排序策略, 跳跃排序,如果有两个同一级别时,接下来是第三级别,例如1,2,2,4
select names,dept,rank() over(partition by dept order by age desc) rank from workers;
结果如下图
6.first order by 按照要求对数据进行筛选,正序排
7.last order by 按照要求对数据进行筛选,倒叙排
下面我们将进行不同的数据获取展示:
只获取所需要的信息,便于统计:
with workers as (
select 'doma' dept ,'zhangsan' names,23 age,4000 salaries from dual
union all
select 'doma' dept ,'lisi' names,35 age,9000 salaries from dual
union all
select 'domb' dept ,'wangwu' names,26 age,6500 salaries from dual
union all
select 'domb' dept ,'zhaoliu' names,28 age,7000 salaries from dual
union all
select 'domb' dept ,'maqi' names,26 age,6000 salaries from dual
union all
select 'doma' dept ,'fengba' names,25 age,6500 salaries from dual
union all
select 'domb' dept ,'sujiu' names,25 age,7000 salaries from dual
)
--获取部门中年龄最小但工资最高的工资信息
select a.dept,max(a.salaries) keep(dense_rank first order by a.age) as max_sal from workers a group by a.dept;
结果如下图
下面是获取人员匹配信息,将所有的信息都展示,便于直观统计
with workers as (
select 'doma' dept ,'zhangsan' names,23 age,4000 salaries from dual
union all
select 'doma' dept ,'lisi' names,35 age,9000 salaries from dual
union all
select 'domb' dept ,'wangwu' names,26 age,6500 salaries from dual
union all
select 'domb' dept ,'zhaoliu' names,28 age,7000 salaries from dual
union all
select 'domb' dept ,'maqi' names,26 age,6000 salaries from dual
union all
select 'doma' dept ,'fengba' names,25 age,6500 salaries from dual
union all
select 'domb' dept ,'sujiu' names,25 age,7000 salaries from dual
)
--获取部门中年龄最小但工资最高的人员信息
select a.*, max(a.salaries) keep(dense_rank first order by a.age ) over(partition by a.dept) as max_sal from workers a ;
结果如下图