Skip to main content

date_bin

Function date_bin 

Source
pub fn date_bin<'a, V, S, E, O>(
    stride: S,
    source: E,
    origin: O,
) -> SQLExpr<'a, V, E::SQLType, <<S::Nullable as Nullability>::Or<E::Nullable> as Nullability>::Or<O::Nullable>, <<S::Aggregate as AggregateKind>::Or<E::Aggregate> as AggregateKind>::Or<O::Aggregate>, (S::Sources, (E::Sources, O::Sources))>
Expand description

Rounds a timestamp down into fixed-size buckets (DATE_BIN), on PostgreSQL 14+.

Buckets are stride wide (an interval as text, such as '15 minutes') and start at origin. source and origin must be temporal. The stride is cast to INTERVAL. The result has source’s type and is nullable if any argument is.

§Examples

let bucket = date_bin("15 minutes", users.created_at, localtimestamp());
assert_eq!(
    bucket.sql(),
    r#"DATE_BIN (CAST ($1 AS INTERVAL), "users"."created_at", LOCALTIMESTAMP)"#
);