`
hehaibo
  • 浏览: 419734 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

lag over()

 
阅读更多

In Oracle/PLSQL, the lag function is an analytic function that lets you query more than one row in a table at a time without having to join the table to itself. It returns values from a previous row in the table. To return a value from the next row, try using the lead function .

The syntax for the lag function is:

lag ( expression [, offset [, default] ] )
over ( [ query_partition_clause ] order_by_clause )

expression is an expression that can contain other built-in functions, but can not contain any analytic functions.

offset is optional. It is the physical offset from the current row in the table. If this parameter is omitted, the default is 1.

default is optional. It is the value that is returned if the offset goes out of the bounds of the table. If this parameter is omitted, the default is null.

 

Applies To:

  • Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g

 

For example:

Let's take a look at an example. If we had an orders table that contained the following data:

ORDER_DATE PRODUCT_ID QTY
25/09/2007 1000 20
26/09/2007 2000 15
27/09/2007 1000 8
28/09/2007 2000 12
29/09/2007 2000 2
30/09/2007 1000 4

And we ran the following SQL statement:

select product_id, order_date,
lag (order_date,1) over (ORDER BY order_date) AS prev_order_date
from orders;

It would return the following result:

PRODUCT_ID ORDER_DATE PREV_ORDER_DATE
1000 25/09/2007 <NULL>
2000 26/09/2007 25/09/2007
1000 27/09/2007 26/09/2007
2000 28/09/2007 27/09/2007
2000 29/09/2007 28/09/2007
1000 30/09/2007 29/09/2007

Since we used an offset of 1, the query returns the previous order_date.

If we had used an offset of 2 instead, it would have returned the order_date from 2 orders before. If we had used an offset of 3, it would have returned the order_date from 3 orders before....and so on.

 

If we wanted only the orders for a given product_id, we could run the following SQL statement:

select product_id, order_date,
lag (order_date,1) over (ORDER BY order_date) AS prev_order_date
from orders
where product_id = 2000;

It would return the following result:

PRODUCT_ID ORDER_DATE PREV_ORDER_DATE
2000 26/09/2007 <NULL>
2000 28/09/2007 26/09/2007
2000 29/09/2007 28/09/2007

In this example, it returned the previous order_date for product_id = 2000 and ignored all other orders.

分享到:
评论

相关推荐

    Oracle查询中OVER (PARTITION BY ..)用法

    Oracle查询中的`OVER (PARTITION BY ..)`是一个窗口函数,它允许我们在数据集上执行计算,但不是在整个结果集上,而是针对每个分区。这部分功能非常强大,可以用于复杂的分析和排序任务,尤其是在处理分组数据时。在...

    ORACLE_OVER函数

    LAG(salary, 1) OVER (PARTITION BY department_id ORDER BY salary DESC) AS prev_salary, LEAD(salary, 1) OVER (PARTITION BY department_id ORDER BY salary DESC) AS next_salary FROM employees; ``` 这个...

    Oracle开发的over函数

    ### Oracle开发中的OVER函数详解 #### 一、Oracle分析函数简介 在Oracle数据库开发中,分析函数是一类非常强大的工具,主要用于实现复杂的查询需求,尤其是在处理大量数据时,能够提供高级的数据汇总、排序和筛选...

    oracle分析函数over_及开窗函数.txt

    本文将详细介绍Oracle中的`OVER`子句以及几种常用的开窗函数,包括`ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`和`LAG()`等。 #### 二、基本概念 - **`OVER`子句**:`OVER`子句用于指定分析函数的作用范围,它可以...

    oracle lag 实现同一字段的前N行的数据和后N行的值

    LAG(profit, N) OVER (PARTITION BY region ORDER BY year) AS last_N_years_exp FROM test; ``` 其中`N`为需要向前偏移的行数。 #### 获取后N行的值 对于`LEAD`函数,同样可以通过调整`offset`参数来获取每个地区...

    oracle求同比,环比函数(LAG与LEAD)的详解

    在示例中,`LAG(SALARY, 1, 0) OVER(PARTITION BY EMPLOYEENO ORDER BY YEARMONTH)` 就是使用LAG函数获取每名员工上一个月的工资,如果不存在上一个月的数据,则返回0。 2. LEAD函数: LEAD函数与LAG相反,它用于...

    oracle函数介绍(6) 著名函数之分析函数.doc

    7. LAG(col[,n][,n]) over([partition_clause] order_by_clause) LAG函数返回当前行之前n行的列值,如果超出范围,可指定返回的默认值,否则默认为NULL。 8. LEAD(col[,n][,n]) over([partition_clause] order_by_...

    Oracle 分析函数RANK(),ROW_NUMBER(),LAG()等的使用方法

    LAG(ename, 1, null) OVER (PARTITION BY deptno ORDER BY ename) ``` 这个表达式会返回每个部门中,按照姓名排序的前一个员工的姓名。如果已经是第一个,那么返回NULL。 在实际应用中,这些函数可以帮助我们...

    Gbase 8s内置函数之窗口函数

    LAG(f_qty) OVER (PARTITION BY f_quarter ORDER BY f_month) AS f_lag, LEAD(f_qty) OVER (PARTITION BY f_quarter ORDER BY f_month) AS f_lead FROM t_sale; ``` 窗口函数可以对数据进行复杂的计算和分析,...

    oracle分析函数在BI分析中应用事例

    聚合函数(over (partition by 字段1, 字段2, 字段3 order by 字段 desc/asc range between 数值/date preceding/flowing and 数值/date preceding/flowing)) ``` 这里,`聚合函数`可以是`sum`, `count`, `avg`, `...

    ORACLE分析函数

    本文将深入探讨其中几个关键的分析函数,包括`RANK()`,`ROW_NUMBER()`以及`LAG()`,并结合`OVER()`子句的使用进行详尽的解释。 首先,`ROW_NUMBER()`函数用于为数据集中的每一行分配一个唯一的整数。这在需要对...

    ch08_ 秋招秘籍 C1

    CASE WHEN DATE_SUB(imp_date, INTERVAL 1 DAY) = LAG(imp_date, 1) OVER (PARTITION BY uid ORDER BY imp_date) THEN LAG(consecutive_group, 1) OVER (PARTITION BY uid ORDER BY imp_date) ELSE NULL END AS ...

    oracle分组排序统计高级用法

    LAG(score) OVER (ORDER BY score) AS prev_score, LEAD(score) OVER (ORDER BY score) AS next_score FROM test_scores; ``` #### 四、总结 通过本文的介绍,我们可以看到Oracle数据库提供了多种强大的工具来...

    Smooth Sync 3.01

    Simple drop in setup. Smooths Rigidbodies and Transforms over the network with ease. Just add the SmoothSync script to any game ob ject ...Performs interpolation and extrapolation to compensate for lag.

    oracle 常用函数下载

    - `LAG(column, offset, default) OVER (PARTITION BY ... ORDER BY ...)` 访问当前行之前指定偏移量的行。 - `LEAD(column, offset, default) OVER (PARTITION BY ... ORDER BY ...)` 反向访问当前行之后的行。 ...

    ORACLE分析函数.pdf

    LAG(LOCAL_FARE, 1) OVER (PARTITION BY NET_TYPE ORDER BY BILL_MONTH) as PREV_LOCAL_FARE, LEAD(LOCAL_FARE, 1) OVER (PARTITION BY NET_TYPE ORDER BY BILL_MONTH) as NEXT_LOCAL_FARE FROM t; ``` 4. 移动...

    Oracle之分析函数讲解及PPT资源.zip

    分析函数主要有RANK()、DENSE_RANK()、ROW_NUMBER()、LEAD()、LAG()、FIRST_VALUE()、LAST_VALUE()、AVG() OVER()、SUM() OVER()、MIN() OVER()、MAX() OVER()等。它们可以分为排名函数、窗口函数和分组计算函数三类...

    Oracle分析函数实践 - blog.docx

    - `LAG()`, `LEAD() OVER`: 在结果集中向前或向后检索值,常用于比较相邻行的数据。 - `RANK() OVER`: 为每个分组中的行分配一个唯一的排名,排名相等时会有空位。 - `DENSE_RANK() OVER`: 类似于`RANK()`, 但排名...

Global site tag (gtag.js) - Google Analytics