oracle 分析函数lag lead
2015-06-23 11:25
411 查看
LAGis an analytic function. It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and a position of the cursor,
LAGprovides access to a row at a given physical offset prior to that position.
For the optional
offsetargument, specify an integer that is greater than zero. If you do not specify
offset, then its default is 1. The optional
defaultvalue is returned if the offset goes beyond the scope of the window. If you do not specify
default, then its default is null.
{
RESPECT|
IGNORE}
NULLSdetermines whether null values of
value_exprare included in or eliminated from the calculation. The default is
RESPECT
NULLS.
You cannot nest analytic functions by using
LAGor any other analytic function for
value_expr. However, you can use other built-in function expressions for
value_expr.
比如有这样一个需求,按员工号排序。显示每个员工的sal的上一个sal是多少。
SELECT empno,ename, sal, LAG(sal,1,sal) over(ORDER BY empno)AS lastone FROM emp
结果如图
1 7369 SMITH 800.00 800
2 7499 ALLEN 1600.00 800
3 7521 WARD 1250.00 1600
4 7566 JONES 2975.00 1250
5 7654 MARTIN 1250.00 2975
6 7698 BLAKE 2850.00 1250
7 7782 CLARK 2450.00 2850
8 7788 SCOTT 3000.00 2450
9 7839 KING 5000.00 3000
10 7844 TURNER 1500.00 5000
11 7876 ADAMS 1100.00 1500
12 7900 JAMES 950.00 1100
13 7902 FORD 3000.00 950
14 7934 MILLER 1300.00 3000
lead则为下一个,如lag
相关文章推荐
- oracle索引
- Oracle 行转列
- ORACLE日期格式
- 向maven中添加 oracle jdbc驱动
- Oracle 如何写出高效的 SQL
- Oracle的分析函数over
- oracle创建表空间
- OGG 12C Oracle to Mysql
- Oracle Hint的用法
- Oracle删除当前用户下所有的表的方法
- Oracle常用数据字典
- 110个常用Oracle函数总结
- 最佳实践(保持、清理ORACLE alert日志)
- ORACLE数据库中创建、删除--同义词、视图、索引、序列
- Oracle数据库之视图与索引
- SQLSERVER 2008 链接 到 ORACLE 11
- Linux Oracle数据库的安装
- 关于oracle数据库索引的一些学习记录
- Oracle 12C -- 使用seed PDB创建新的pdb
- Oracle数据库之rownum