Syntax
LEAD { ( value_expr [, offset [, default]]) [ { RESPECT | IGNORE } NULLS ] | ( value_expr [ { RESPECT | IGNORE } NULLS ] [, offset [, default]] )} OVER ([ query_partition_clause ] order_by_clause)
Purpose
The LEAD() window function allows you to retrieve data from the Nth row that follows the current row in the same column within a query. While this can be achieved using self-joins on the same table, LEAD() offers a more efficient approach.
Parameters
The following table describes the parameters of the LEAD() function.
Parameter |
Description |
|---|---|
value_expr |
The column to compare. |
offset |
The offset of value_expr. |
default |
The default value to return. If not specified, it defaults to NULL. |
[ { RESPECT | IGNORE } NULLS ] |
Specifies whether to consider NULL values. The default is RESPECT NULLS, which considers NULL values. |
order_by_clause |
Indicates the column by which data should be sorted to determine the rows before and after the current row. |
query_partition_clause |
Specifies the query partition. If not provided, it defaults to using global data. |
Examples
obclient> CREATE TABLE EXPLOYEES(LAST_NAME CHAR(10), SALARY DECIMAL, JOB_ID CHAR(32));
Query OK, 0 rows affected
obclient> INSERT INTO EXPLOYEES VALUES('JIM', 2000, 'CLEANER');
Query OK, 1 row affected
obclient> INSERT INTO EXPLOYEES VALUES('MIKE', 12000, 'ENGINEERING');
Query OK, 1 row affected
obclient> INSERT INTO EXPLOYEES VALUES('LILY', 13000, 'ENGINEERING');
Query OK, 1 row affected
obclient> INSERT INTO EXPLOYEES VALUES('TOM', 11000, 'ENGINEERING');
Query OK, 1 row affected
obclient> SELECT LAST_NAME, LEAD(SALARY) OVER(ORDER BY SALARY) LEAD, LAG(SALARY) OVER(ORDER BY SALARY) LAG FROM EXPLOYEES;
+-----------+-------+-------+
| LAST_NAME | LEAD | LAG |
+-----------+-------+-------+
| JIM | 11000 | NULL |
| TOM | 12000 | 2000 |
| MIKE | 13000 | 11000 |
| LILY | NULL | 12000 |
+-----------+-------+-------+
4 rows in set
