Product Solutions Support
Try MemSQL

LAG

Value function. Returns an expression evaluated at some offset before the current row. If there is no such row returns NULL instead.

LAG ( expression [, offset]) OVER (
    [PARTITION BY (col | expr), ...] 
    [ORDER BY (col | expr), ...]
)

Arguments

Return Type

The result of expression

Examples

memsql> create table example (a int, b int);

memsql> insert into example values(1,2),(2,2),(3,3),(4,3);

memsql> select a,lag(a) over(order by (a)) from example;
+------+---------------------------+
| a    | lag(a) over(order by (a)) |
+------+---------------------------+
|    1 |                      NULL |
|    2 |                         1 |
|    3 |                         2 |
|    4 |                         3 |
+------+---------------------------+

memsql> select a,lag(a,2) over(order by (a)) from example;
+------+-----------------------------+
| a    | lag(a,2) over(order by (a)) |
+------+-----------------------------+
|    1 |                        NULL |
|    2 |                        NULL |
|    3 |                           1 |
|    4 |                           2 |
+------+-----------------------------+
Was this article useful?