Compare with the neighbouring row
Compare an order with the previous or next row in a sequence. LAG and LEAD avoid a long self-join but need a clear order. An absent neighbour does not mean zero, and a previous row is not necessarily yesterday.
LAG reads a previous row and LEAD a following row within a partition. Offset counts rows; default applies when the neighbour row is absent, not when an existing value is NULL.
- Compare with the neighbouring row
- LAG reads a previous row and LEAD a following row within a partition. Offset counts rows; default applies when the neighbour row is absent, not when an existing value is NULL.