lag_with_offset

Function lag_with_offset 

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

Value of argument from row lagging current row within partition

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

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_with_offset(id, 1)
            .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);