Sub topic 13
Calculating period-over-period change
This chapter assumes you can write an OVER clause with PARTITION BY and ORDER BY, and that you know how LAG and LEAD take an offset and a default value. Here you combine them into a standard reporting query: how much a value changed compared with an earlier period, in absolute terms and as a percentage.
The idea
A period-over-period change compares each row with the row for an earlier period. LAG fetches that earlier row's value. Subtract it from the current value to get the change. Divide the change by the earlier value to get the percentage.
Sample data
The examples use a table with one row per region and month. The primary key guarantees there is exactly one row per region per month, which matters later.
CREATE TABLE monthly_sales (
region VARCHAR2(10) NOT NULL,
sales_month DATE NOT NULL,
amount NUMBER(10,2) NOT NULL,
CONSTRAINT monthly_sales_pk PRIMARY KEY (region, sales_month)
);
INSERT ALL
INTO monthly_sales VALUES ('EAST', DATE '2026-01-01', 100)
INTO monthly_sales VALUES ('EAST', DATE '2026-02-01', 120)
INTO monthly_sales VALUES ('EAST', DATE '2026-03-01', 90)
INTO monthly_sales VALUES ('EAST', DATE '2026-04-01', 90)
INTO monthly_sales VALUES ('EAST', DATE '2026-05-01', 135)
INTO monthly_sales VALUES ('WEST', DATE '2026-01-01', 50)
INTO monthly_sales VALUES ('WEST', DATE '2026-02-01', 0)
INTO monthly_sales VALUES ('WEST', DATE '2026-03-01', 40)
SELECT * FROM dual;
COMMIT;Absolute and percentage change
Call LAG once in an inline view, then do the arithmetic in the outer query. This avoids repeating the same analytic expression and keeps the logic readable.
SELECT region,
sales_month,
amount,
prev_amount,
amount - prev_amount AS change_abs,
ROUND(100 * (amount - prev_amount) / NULLIF(prev_amount, 0), 1) AS change_pct
FROM (
SELECT region,
sales_month,
amount,
LAG(amount) OVER (
PARTITION BY region
ORDER BY sales_month
) AS prev_amount
FROM monthly_sales
)
ORDER BY region, sales_month;The result:
| REGION | SALES_MONTH | AMOUNT | PREV_AMOUNT | CHANGE_ABS | CHANGE_PCT |
|---|---|---|---|---|---|
| EAST | 2026-01-01 | 100 | NULL | NULL | NULL |
| EAST | 2026-02-01 | 120 | 100 | 20 | 20 |
| EAST | 2026-03-01 | 90 | 120 | -30 | -25 |
| EAST | 2026-04-01 | 90 | 90 | 0 | 0 |
| EAST | 2026-05-01 | 135 | 90 | 45 | 50 |
| WEST | 2026-01-01 | 50 | NULL | NULL | NULL |
| WEST | 2026-02-01 | 0 | 50 | -50 | -100 |
| WEST | 2026-03-01 | 40 | 0 | 40 | NULL |
Four details in this output come from choices in the query.
- PARTITION BY region restarts the comparison for each region. Without it, WEST's January row would be compared with EAST's May row.
- The first row of each partition has no previous row. LAG returns NULL there, so the change is NULL too. This is the correct answer: there is nothing to compare against.
- NULLIF(prev_amount, 0) turns a zero denominator into NULL. Without it, WEST's March row would raise ORA-01476 (divisor is equal to zero). The percentage is undefined when the earlier value is zero, and NULL says so honestly.
- The window ORDER BY is on the DATE column. Ordering by a string such as
TO_CHAR(sales_month, 'MON')sorts alphabetically (APR, FEB, JAN) and gives meaningless "previous" rows.
Choosing the default argument carefully
The third argument of LAG replaces the NULL that appears when no previous row exists. It is tempting to write LAG(amount, 1, 0), but think about what that does to the first period.
SELECT region,
sales_month,
amount,
amount - LAG(amount, 1, 0) OVER (
PARTITION BY region
ORDER BY sales_month
) AS change_with_default
FROM monthly_sales
ORDER BY region, sales_month;For EAST in January, the change becomes 100 - 0 = 100. That reports a jump of 100 in a month where nothing was compared. Any chart or total built from that column will include an invented change.
Looking forward with LEAD
LEAD gives the same comparison in the other direction. It is useful for a forecast-style question such as "how much will the next month change from this one?" The change is attached to the earlier of the two rows.
SELECT region,
sales_month,
amount,
LEAD(amount) OVER (
PARTITION BY region
ORDER BY sales_month
) - amount AS change_to_next
FROM monthly_sales
ORDER BY region, sales_month;The last row of each region returns NULL, for the same reason the first row did with LAG.
Comparing with the same month last year
A year-over-year comparison uses an offset of 12 when the data has one row per month.
SELECT region,
sales_month,
amount,
LAG(amount, 12) OVER (
PARTITION BY region
ORDER BY sales_month
) AS same_month_last_year
FROM monthly_sales
ORDER BY region, sales_month;The offset counts rows, not calendar time. It is only correct if every month is present in each partition. If July 2025 is missing, LAG(amount, 12) for July 2026 returns the value from August 2025, and no error warns you. Check for gaps before relying on a row offset. There are two ways to handle them:
- Fill the gaps first. Generate every month for every region and outer join the sales to that list, so each partition has exactly one row per month. Missing months then show up as NULL amounts.
- Skip LAG and join the table to itself on a date calculation, for example
ON prev.region = cur.region AND prev.sales_month = ADD_MONTHS(cur.sales_month, -12). A missing month gives no match, so the result is NULL instead of a wrong value.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| ORA-01476 divisor is equal to zero | The earlier value can be 0 | Wrap the denominator in NULLIF(x, 0) |
| Change compares different regions | PARTITION BY is missing | Add the grouping column to PARTITION BY |
| Changes look random | Window ORDER BY uses text or a non-chronological column | Order by the DATE column itself |
| Different results on repeated runs | Ties in the ORDER BY column, for example two rows with the same date in a partition | Add a tiebreaker column, or aggregate to one row per period first |
| ORA-30483 window functions are not allowed here | LAG used in WHERE | Compute it in an inline view and filter in the outer query |
| First row shows a large change | Default of 0 in LAG | Remove the default and keep NULL |
If your table can hold several rows per region and month, aggregate first with GROUP BY in a subquery, then apply LAG to the monthly totals. LAG on raw rows would compare one transaction with the previous transaction, not one month with the previous month.
Exercise
Using monthly_sales, write a query that returns, for each region and month, the amount and the percentage change from the previous month, rounded to 1 decimal place. Rows with no previous month, or with a previous amount of 0, must show NULL for the percentage. Then explain why LAG(amount, 1, 0) would be a poor choice here.
Show answer
SELECT region, salesmonth, amount, ROUND(100 * (amount - prevamount) / NULLIF(prevamount, 0), 1) AS changepct FROM (SELECT region, salesmonth, amount, LAG(amount) OVER (PARTITION BY region ORDER BY salesmonth) AS prevamount FROM monthlysales) ORDER BY region, salesmonth; With a default of 0, the first month of each region would have prevamount = 0. NULLIF would still return NULL for the percentage, but an absolute change of amount - 0 would report the whole first amount as growth. Keeping the default NULL makes "no earlier period" distinct from "earlier period had a value of zero".
Exercise
A table has one row per month for each region, except that EAST has no row for 2025-07-01. What does LAG(amount, 12) OVER (PARTITION BY region ORDER BY sales_month) return for EAST on 2026-07-01, and how can you get a correct year-over-year comparison?
Show answer
It returns the amount from 2025-08-01, because the offset counts rows and the missing July row shifts every later row by one position. Nothing raises an error. To get a correct result, either densify the data so every month exists for every region (outer join to a generated list of months), or self-join the table on prev.salesmonth = ADDMONTHS(cur.sales_month, -12), which returns NULL when the earlier month is absent.