• Oracle/PLSQL: Lead Function


    In Oracle/PLSQL, the lead 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 the next row in the table. To return a value from a previous row, try using the lag function.

    Syntax

    The syntax for the lead function is:

    lead ( 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 11g, Oracle 10g, Oracle 9i, Oracle 8i

    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,
    lead (order_date,1) over (ORDER BY order_date) AS next_order_date
    from orders;

    It would return the following result:

    PRODUCT_ID

    ORDER_DATE

    NEXT_ORDER_DATE

    1000

    25/09/2007

    26/09/2007

    2000

    26/09/2007

    27/09/2007

    1000

    27/09/2007

    28/09/2007

    2000

    28/09/2007

    29/09/2007

    2000

    29/09/2007

    30/09/2007

    1000

    30/09/2007

    <NULL>

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

    If we had used an offset of 2 instead, it would have returned the order_date from 2 orders later. If we had used an offset of 3, it would have returned the order_date from 3 orders later....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,
    lead (order_date,1) over (ORDER BY order_date) AS next_order_date
    from orders
    where product_id = 2000;

    It would return the following result:

    PRODUCT_ID

    ORDER_DATE

    NEXT_ORDER_DATE

    2000

    26/09/2007

    28/09/2007

    2000

    28/09/2007

    29/09/2007

    2000

    29/09/2007

    <NULL>

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

  • 相关阅读:
    学习OpenCV——cv::inpaint函数(三)
    深度学习推荐系统架构、Sparrow RecSys项目及深度学习基础知识
    【zabbix Java开发教程】docker部署zabbix及api获取实战教程
    ubuntu下Anaconda安装与使用教程
    教程三 在Go中使用Energy创建跨平台应用 - 状态控制
    “卖身”英伟达失败后,Arm 又被高通看上?
    十一:以理论结合实践方式梳理前端 React 框架 ———框架架构
    Mybatis的优缺点
    图片转world文档 Excel excel
    监听div元素高度变化的方案
  • 原文地址:https://blog.csdn.net/yuanlnet/article/details/125485080