Avoid NULL on the first row.
Section: LAG and LEAD basics
Provide a LAG default value
sql
sql
SELECT day,
revenue,
LAG(revenue, 1, 0) OVER (ORDER BY day) AS prev_revenue_or_zero
FROM daily_revenue;Explanation
The third argument is the default when an offset row does not exist.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing LAG and LEAD basics