LEAD
Description
The LEAD function is a type of window function used to access a row at a specified offset from the current row. By using the LEAD function, you can obtain the value of an expression for a row that is a specified number of rows ahead in the partition. This is very useful in data analysis and processing, such as comparing the data difference between the current row and the next row.
Syntax
Parameters
expr
: An expression of any type, which can be a column name, constant, function, etc.offset
: Optional parameter, a bigint type constant, default value is 1, indicating the row after the current row. When offset is 0, it indicates the current row itself.default
: Optional parameter, the default value used when the offset exceeds the boundary of the window. The type is the same as expr, and the default value is null.
Return Result
The return value type is the same as expr.
Usage Example
- Basic Usage
Results:
- Using offset and default parameters
Results:
Notes
- When the offset value is greater than the number of rows in the window, the LEAD function will return the value specified by default.
- The LEAD function is typically used with the ORDER BY clause of window functions to determine how to access related rows.
- When using the LEAD function, ensure that the PARTITION BY clause is set correctly to partition the data properly.