MySQL LAG Function
The LAG()
function in MySQL is a window function introduced in MySQL 8.0. It allows you to access data from a previous row in the same result set without requiring a self-join. This is useful for comparing values in consecutive rows or analyzing trends over time.
Syntax
expression
: The column or calculation whose value is returned from a previous row.offset
: The number of rows behind the current row to look for the value (default is1
).default
: The value returned if no previous row exists (default isNULL
).PARTITION BY
: Divides the result set into partitions; the function is applied to each partition independently.ORDER BY
: Specifies the order of rows in which the function is applied.
Key Points
Default Values:
- If no previous row exists, the function returns
NULL
unless adefault
value is specified.
- If no previous row exists, the function returns
Offset:
- The
offset
determines how far back the function looks for the value. - By default,
offset
is1
.
- The
Partitioning:
- The result set can be partitioned into groups using the
PARTITION BY
clause, and the function will operate within each group.
- The result set can be partitioned into groups using the
Ordering:
- The
ORDER BY
clause ensures rows are processed in a specific order.
- The
Example 1: Simple Use of LAG()
Suppose you have a table sales
:
sale_id | region | amount |
---|---|---|
1 | North | 100 |
2 | North | 200 |
3 | North | 150 |
4 | South | 300 |
5 | South | 250 |
To fetch the current amount and the previous amount:
Output:
sale_id | region | amount | previous_amount |
---|---|---|---|
1 | North | 100 | NULL |
3 | North | 150 | 100 |
2 | North | 200 | 150 |
5 | South | 250 | NULL |
4 | South | 300 | 250 |
Example 2: Using LAG()
with an Offset
To fetch the amount two rows behind:
Output:
sale_id | region | amount | two_rows_behind |
---|---|---|---|
1 | North | 100 | NULL |
3 | North | 150 | NULL |
2 | North | 200 | 100 |
5 | South | 250 | NULL |
4 | South | 300 | NULL |
Example 3: Using LAG()
with a Default Value
To provide a default value when no previous row exists:
Output:
sale_id | region | amount | previous_amount_with_default |
---|---|---|---|
1 | North | 100 | 0 |
3 | North | 150 | 100 |
2 | North | 200 | 150 |
5 | South | 250 | 0 |
4 | South | 300 | 250 |
Example 4: Calculate Change Between Rows
Using the LAG()
function to calculate the difference between the current amount and the previous amount:
Output:
sale_id | region | amount | previous_amount | amount_difference |
---|---|---|---|---|
1 | North | 100 | NULL | NULL |
3 | North | 150 | 100 | 50 |
2 | North | 200 | 150 | 50 |
5 | South | 250 | NULL | NULL |
4 | South | 300 | 250 | 50 |
Use Cases
Comparative Analysis:
- Compare current values to past values within a dataset.
Trend Analysis:
- Analyze changes in metrics like sales or prices over time.
Gaps in Data:
- Identify and analyze gaps between consecutive rows.
Historical Context:
- Access past data to provide context for the current row.
Conclusion
The LAG()
function is a powerful tool for accessing previous rows in a result set. It simplifies tasks like comparisons, trend analysis, and identifying gaps without requiring complex subqueries or joins. Its flexibility with partitioning and ordering makes it essential for advanced analytics in MySQL.