Overview
TheLAG()
window function returns the values from specific rows based on the offset argument (previous to the current row in the partition). It can be used with all data types supported by Oxla
Syntax
The syntax for this function is as follows:Parameters
expression
: column, which will be referencedoffset
: numeric indicator of the previous row to access, that is relative to the current row (optional, if not specified 1 will be returned)default
: value that wil be returned if theoffset
is out of range (optional, if not specifiedNULL
will be returned)
Examples
For the needs of this section, we will create thewinsales
table that stores details about some sales transactions:
LAG(expression, offset)
In this example, we will focus on executing theLAG()
function with expression and offset parameters’ values specified:
c
:
LAG(expression, offset, default)
In this example, we will focus on executing theLAG()
function with expression, offset and default parameters’ values specified:
Time Series: LAG() to Compare Daily Sales Quantities
In this example, we will use LAG() to compare each day’s sales quantity (qty
) with the previous day’s quantity, ordered by dateid
: