現在行からn行前の値を返します。
構文
LAG(expr, n, default) OVER (w)使用例
下記の値を入力するとサンプルに即時反映されます。
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;