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_DATEPRODUCT_IDQTY
25/09/2007100020
26/09/2007200015
27/09/200710008
28/09/2007200012
29/09/200720002
30/09/200710004

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_IDORDER_DATEPREV_ORDER_DATE
100025/09/2007<NULL>
200026/09/200725/09/2007
100027/09/200726/09/2007
200028/09/200727/09/2007
200029/09/200728/09/2007
100030/09/200729/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_IDORDER_DATEPREV_ORDER_DATE
200026/09/2007<NULL>
200028/09/200726/09/2007
200029/09/200728/09/2007

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Oracle中的LAG函数是一种窗口函数,用于在查询结果中返回当前行之前的某一行的值。OVER PARTITION BY子句用于将查询结果分组,以便在每个分组内计算LAG函数。这样,可以在每个分组内找到当前行之前的某一行的值。 ### 回答2: Oracle窗口函数中的LAG函数是一种可以用于计算前一行的值的函数。在使用LAG函数时,您需要指定要在哪一列上执行该函数以及要在哪一行上执行该函数。 在Oracle中,LAG函数还可以通过在其后面添加一个PARTITION BY子句来进行分组操作。当使用PARTITION BY子句时,LAG函数将仅在指定的分组中执行,并计算前一行的值。 例如,如果您想要计算每个部门的员工薪水与前一个雇员的薪水之差,您可以使用以下SQL查询: SELECT dept_id, emp_id, salary, LAG(salary, 1) OVER (PARTITION BY dept_id ORDER BY emp_id) AS prev_salary FROM employees; 在上面的查询中,我们使用了LAG函数来计算前一个雇员的工资。由于我们使用了PARTITION BY部门ID,因此LAG函数只会在每个部门的员工中执行,并计算前一个雇员的薪水。 此外,我们还在LAG函数中指定了第二个参数1,以便将其设置为向上查找一行。这意味着我们只计算前一个雇员的薪水,而不是前两个或更多个雇员的薪水。 总之,OracleLAG函数是一个强大的窗口函数,可用于计算前一行的值,并且可以使用PARTITION BY子句进行分组操作。使用LAG函数,可以轻松地计算出数据集中任意列的前一个值,并在一次查询中同时计算多个值。 ### 回答3: 在Oracle数据库中,Lag()函数是一种分析函数,可以在查询结果集中,获取当前行之前或之后的行数据。其常用的语法形式为:LAG(col_name,offset[,default]) OVER ([PARTITION BY col_name [,...n]] ORDER BY col_name [,...n])) 其中,col_name表示要获取的列名,offset表示偏移量,也就是获取当前行之前或之后的行数,default表示当没有数据可获取时,返回的默认值(可选)。 Partition by子句可以将结果集按照一个或多个列进行分组,这样Lag()函数就可以在每个分组内部进行计算。因此,Lag over partition by的作用就是在特定条件下,对查询结果的分组进行数据复制和移位,以便执行差异计算等需求。 举个例子来说,我们可以使用Lag()函数进行数据统计。比如,我们要统计每个人的年龄与上一次年龄的差值,可以如下编写SQL语句: SELECT name, age, age-LAG(age) OVER (ORDER BY name) AS age_diff FROM people 其中,name是分组列,age是要统计的数据列,age-Lag()得到的是当前行数据与上一行数据的差值,OVER子句中的ORDER BY按照name列进行排序。 使用Lag over partition by时,在PARTITION BY子句中指定分组列,如下所示: SELECT name, age, age-LAG(age) OVER (PARTITION BY gender ORDER BY name) AS age_diff FROM people 这样就可以根据gender分组统计年龄差值,而不是在整个结果集中进行计算。 总之,Oracle Lag over partition by是一种强大的分析函数,可以实现多种数据分组和移位计算等需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值