Does SQL LAG Compare Yesterday or Just the Previous Recorded Row?
A daily operations report shows a dramatic increase on Wednesday. The calculation subtracts the previous value with SQL LAG, and the column is labelled “change since yesterday.” But the source contains no Tuesday row. The arithmetic can be correct while the label is wrong: Wednesday is being compared with Monday.
This is a question about the meaning of the comparison, not simply about selecting a different function. Before changing the query, decide whether the reader needs the previous observation, the previous calendar day or the previous business day. Each requires a different definition of the eligible history.
Understand what LAG actually moves across
PostgreSQL's window-function reference defines LAG in terms of rows within a partition. Its default offset is one row. If that row does not exist, the default result is NULL unless another default has been supplied.
The function does not create missing dates. Sorting observations by date makes the preceding row chronological, but does not establish a daily calendar. A report can contain gaps, duplicate dates or several measurements within one day.
Write the intended unit beside the expression. “Previous recorded observation for this team” is a useful specification. “Yesterday” requires additional evidence that the referenced date really is yesterday.
Build a fixture with a deliberate gap
Illustrative example: Monday and Wednesday
Suppose a fictional team records 12 completed requests on Monday and 18 on Wednesday. There is no Tuesday observation. LAG of the count returns 12 for Wednesday, so the difference is 6. That is a change from the previous recorded observation, across a two-day gap.
There is no evidence here that Tuesday had zero completions. Filling the missing row with zero would change the Wednesday comparison to 18 and make a stronger assumption than the source supports.
Include both the previous value and previous date in a review extract. Seeing Monday beside Wednesday makes the discrepancy easier to detect than inspecting the difference column alone.
Add a second team and a duplicate date
Give another team its own observations and ensure the comparison stays within that team's partition. Then add two Wednesday records for the first team. Decide whether they should be aggregated into one daily result, represent separate measurements, or be rejected as duplicates.
Do not solve an undefined daily grain with an arbitrary tie-breaker. A deterministic order cannot make two competing daily totals represent one agreed measurement.
Choose a calendar strategy deliberately
For a calendar-day comparison, one practical design is to join each daily result to the record for the exact preceding date. Another is to build a complete calendar, attach observed values, and then compare adjacent dates. These are design options, not interchangeable shortcuts.
A business-day comparison needs the organization's working calendar. A Friday-to-Monday comparison may be appropriate, but public holidays and local operating schedules need explicit treatment. Do not infer them from the presence or absence of records.
When no required comparison value exists, show an unavailable comparison or a clearly named alternative. A fallback to the previous available observation should remain visible to the reader.
Keep missingness separate from activity
Record why a date is missing when the process can establish that reason: no collection occurred, the source was late, the team was closed, or a zero-activity observation was explicitly recorded. These states need not lead to the same display.
A daily report should also specify its date boundary. If the source stores timestamps, establish which reporting time zone assigns them to dates before creating daily groups. Otherwise two teams can appear to share a date while covering different intervals.
Test the first observation, consecutive dates, a missing date, an explicit zero and a late correction. Check both the numerical result and the text explaining its comparison period.
Make the label part of acceptance
Accept the report only when a reviewer can identify the current date, comparison date and handling of missing observations. Keep the gap fixture with the query so future refactoring cannot quietly restore a misleading “yesterday” label.
For the underlying distinction, read missing observations versus recorded zero. A useful FutureOS project question includes the desired reporting interval and a tiny dataset where the current interpretation fails.
Editorial note
Editorial note: This article was prepared with AI assistance. Primary sources and the linked FutureOS article inventory were checked on October 26, 2026. Examples are illustrative and are not customer results. The current FutureOS introduction describes the project as being in prelaunch; this guide does not assert that a particular product feature, integration or service is available. The procedures are practical proposals to adapt and test in your own setting. No independent expert review is claimed. Start with a specific question, your relevant constraints and a small example of the result you need.




Comments