sql 位置偏移函数

2021/12/29 19:10:36

本文主要是介绍sql 位置偏移函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

      位置偏移函数用于返回分组内相对位置的成员,主要有FIRST_VALUE、LAST_VALUE、LEAD、LAG,后面跟 over (partition by 分组列名 order by 排序列名)子句。

    FIRST_VALUE( ):用于返回分组内首项成员记录

    LAST_VALUE( ):用于返回分组内尾项成员记录

    LEAD(N):用于返回分组内成员当前行的后N行记录

    LAG( M):用于返回分组内成员当前行的前M行记录

 

select t.employee_id, t.first_name,t2.department_name,t.salary,
first_value(t.salary) over ( partition by t.department_id order by t.salary rows between unbounded preceding and unbounded following) as first_value,
last_value(t.salary) over ( partition by t.department_id order by t.salary rows between unbounded preceding and unbounded following) as last_value,
lead(t.salary,1) over ( partition by t.department_id order by t.salary ) as lead_value,
lag(t.salary,1) over ( partition by t.department_id order by t.salary ) as lag_value
from hr.employees t,hr.departments t2 where t.department_id=t2.department_id

 

 



这篇关于sql 位置偏移函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程