lag

Function lag 

Source
pub fn lag<T: SqlType + SingleValue + IntoNullable<Nullable: SingleValue>, value>(
    value: value,
) -> lag<T, <value as AsExpression<T>>::Expression>
where value: AsExpression<T>,
Expand description

Value of argument from row lagging current row within partition

Returns value evaluated at the row that is one row before the current row within the partition. If there is no such row, NULL is returned instead.

See lag_with_offset and lag_with_offset_and_default for variants with configurable offset and default values.

This function must be used as window function. You need to call at least one of the methods WindowExpressionMethods from to use this function in your SELECT clause. It cannot be used outside of SELECT clauses.

For MySQL this function requires you to call .window_order()

let res = posts
    .select((
        title,
        user_id,
        lag(id).partition_by(user_id).window_order(user_id),
    ))
    .load::<(String, i32, Option<i32>)>(connection)?;
let expected = vec![
    ("My first post".to_owned(), 1, None),
    ("About Rust".into(), 1, Some(1)),
    ("My first post too".into(), 2, None),
];
assert_eq!(expected, res);