pub fn lag_with_offset_and_default<T: SqlType + SingleValue + IntoNotNullable<NotNullable: SameType<T2::NotNullable>> + CombinedNullableValue<T2, T::NotNullable>, T2: SqlType + SingleValue + IntoNotNullable, value, offset, default>(
value: value,
offset: offset,
default: default,
) -> lag_with_offset_and_default<T, T2, <value as AsExpression<T>>::Expression, <offset as AsExpression<Integer>>::Expression, <default as AsExpression<T2>>::Expression>
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, instead returns default (which must be of a type compatible with value). Both offset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to NULL.
This function returns a nullable value if either the value or the default expression are nullable.
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_and_default(id, 1, user_id)
.partition_by(user_id)
.window_order(user_id),
))
.load::<(String, i32, i32)>(connection)?;
let expected = vec![
("My first post".to_owned(), 1, 1),
("About Rust".into(), 1, 1),
("My first post too".into(), 2, 2),
];
assert_eq!(expected, res);
let res = posts
.select((
title,
user_id,
lag_with_offset_and_default(None::<i32>.into_sql::<Nullable<Integer>>(), 1, user_id)
.partition_by(user_id)
.window_order(user_id),
))
.load::<(String, i32, Option<i32>)>(connection)?;
let expected = vec![
("My first post".to_owned(), 1, Some(1)),
("About Rust".into(), 1, None),
("My first post too".into(), 2, Some(2)),
];
assert_eq!(expected, res);
let res = posts
.select((
title,
user_id,
lag_with_offset_and_default(id, 1, None::<i32>.into_sql::<Nullable<Integer>>())
.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);