Sub topic 12
Offset and Default Arguments in Oracle Analytic Functions
This chapter assumes you know SELECT, ORDER BY, inline views, and the OVER clause. It focuses on LAG and LEAD arguments that control how far the function looks and what it returns when no matching row exists.
The three arguments
LAG reads a value from an earlier row. LEAD reads a value from a later row. Their general form is:
LAG(value_expression, offset, default_value)
OVER (analytic_clause)
LEAD(value_expression, offset, default_value)
OVER (analytic_clause)value_expressionis the column or expression to read.offsetis the number of rows to move. The default is1.default_valueis returned when the requested row is outside the partition. The default isNULL.
The analytic ORDER BY determines what “earlier” and “later” mean.
| Function | Offset direction | Example with offset 2 |
|---|---|---|
| `LAG` | Earlier rows | Value from two rows before |
| `LEAD` | Later rows | Value from two rows after |
Using an offset
Suppose monthly_sales contains one row per month. This query reads the amount from two months earlier and two months later:
SELECT sales_month,
amount,
LAG(amount, 2) OVER (ORDER BY sales_month) AS amount_two_months_ago,
LEAD(amount, 2) OVER (ORDER BY sales_month) AS amount_two_months_ahead
FROM monthly_sales
ORDER BY sales_month;For the first two rows, LAG(amount, 2) has no earlier row two positions away, so it returns NULL. Similarly, LEAD(amount, 2) returns NULL for the last two rows.
An offset is a row position, not a time interval. If a month is missing from the table, offset 1 means the previous stored row, not necessarily the previous calendar month.
Supplying a default
You can replace the out-of-range NULL with a value:
SELECT sales_month,
amount,
LAG(amount, 1, 0) OVER (ORDER BY sales_month) AS previous_amount,
LEAD(amount, 1, 0) OVER (ORDER BY sales_month) AS next_amount,
amount - LAG(amount, 1, 0) OVER (ORDER BY sales_month) AS change_from_previous
FROM monthly_sales
ORDER BY sales_month;The first row receives 0 for previous_amount. The last row receives 0 for next_amount. This makes arithmetic easier, but it also changes the meaning of “no previous value” into zero.
The default applies only when the requested row is outside the window. If the matching row exists but its amount is NULL, the result remains NULL.
Partitions reset the offset
With PARTITION BY, the function cannot cross a group boundary:
SELECT department_id,
employee_id,
salary,
LAG(salary, 1, 0) OVER (
PARTITION BY department_id
ORDER BY salary DESC, employee_id
) AS previous_salary
FROM employees
ORDER BY department_id, salary DESC, employee_id;The first employee in every department receives 0, because there is no earlier row inside that department.
Avoiding ambiguous results
Add a tiebreaker to the window ORDER BY when the main sort column can tie. For example, use ORDER BY event_time, event_id rather than only event_time. Otherwise Oracle may choose either tied row as the neighbour, making the result nondeterministic.
Exercise
What does LAG(amount, 2, 0) return for the second row in an ordered partition?
Show answer
It returns 0 because the row two positions earlier is outside the partition.