Returns the value from n rows before the current row.
Syntax
LAG(expr, n, default) OVER (w)Example
Enter values below to update the example in real time.
order_month→revenue→prev_revenue→diff→monthly_sales→-- 전월 대비 매출 차이
SELECT order_month, revenue,
LAG(revenue, 1, 0) OVER (ORDER BY order_month) AS prev_revenue,
revenue - LAG(revenue, 1, 0) OVER (ORDER BY order_month) AS diff
FROM monthly_sales;