pub struct Aggregate<'outer, 'inner, S> { /* private fields */ }Expand description
This is the argument type used for aggregate.
Implementations§
Source§impl<'outer, 'inner, S: 'static> Aggregate<'outer, 'inner, S>
impl<'outer, 'inner, S: 'static> Aggregate<'outer, 'inner, S>
Sourcepub fn avg(
&self,
val: impl IntoExpr<'inner, S, Typ = f64>,
) -> Expr<'outer, S, Option<f64>>
pub fn avg( &self, val: impl IntoExpr<'inner, S, Typ = f64>, ) -> Expr<'outer, S, Option<f64>>
Return the average value in a column, this is None if there are zero rows.
for x in [1, 2, 3] {
txn.insert_ok(Val { x });
}
let (avg1, avg2) = txn.query_one(aggregate(|rows| {
let val = rows.join(Val);
let avg1 = rows.avg(val.x.to_f64());
rows.filter(false); // remove all rows
let avg2 = rows.avg(val.x.to_f64());
(avg1, avg2)
}));
assert_eq!(avg1, Some(2.0));
assert_eq!(avg2, None);Sourcepub fn max<T>(
&self,
val: impl IntoExpr<'inner, S, Typ = T>,
) -> Expr<'outer, S, Option<T>>where
T: EqTyp,
pub fn max<T>(
&self,
val: impl IntoExpr<'inner, S, Typ = T>,
) -> Expr<'outer, S, Option<T>>where
T: EqTyp,
Return the maximum value in a column, this is None if there are zero rows.
for x in [-100, 10, 42] {
txn.insert_ok(Val { x });
}
let (max1, max2) = txn.query_one(aggregate(|rows| {
let val = rows.join(Val);
let max1 = rows.max(&val.x);
rows.filter(false); // remove all rows
let max2 = rows.max(&val.x);
(max1, max2)
}));
assert_eq!(max1, Some(42));
assert_eq!(max2, None);Sourcepub fn min<T>(
&self,
val: impl IntoExpr<'inner, S, Typ = T>,
) -> Expr<'outer, S, Option<T>>where
T: EqTyp,
pub fn min<T>(
&self,
val: impl IntoExpr<'inner, S, Typ = T>,
) -> Expr<'outer, S, Option<T>>where
T: EqTyp,
Return the minimum value in a column, this is None if there are zero rows.
for x in [-100, 10, 42] {
txn.insert_ok(Val { x });
}
let (min1, min2) = txn.query_one(aggregate(|rows| {
let val = rows.join(Val);
let min1 = rows.min(&val.x);
rows.filter(false); // remove all rows
let min2 = rows.min(&val.x);
(min1, min2)
}));
assert_eq!(min1, Some(-100));
assert_eq!(min2, None);Sourcepub fn sum<T>(
&self,
val: impl IntoExpr<'inner, S, Typ = T>,
) -> Expr<'outer, S, T>where
T: NumTyp,
pub fn sum<T>(
&self,
val: impl IntoExpr<'inner, S, Typ = T>,
) -> Expr<'outer, S, T>where
T: NumTyp,
Return the sum of a column.
for x in [-100, 10, 42] {
txn.insert_ok(Val { x });
}
let (sum1, sum2) = txn.query_one(aggregate(|rows| {
let val = rows.join(Val);
let sum1 = rows.sum(&val.x);
rows.filter(false); // remove all rows
let sum2 = rows.sum(&val.x);
(sum1, sum2)
}));
assert_eq!(sum1, -48);
assert_eq!(sum2, 0);Sourcepub fn count_distinct<T: EqTyp + 'static>(
&self,
val: impl IntoExpr<'inner, S, Typ = T>,
) -> Expr<'outer, S, i64>
pub fn count_distinct<T: EqTyp + 'static>( &self, val: impl IntoExpr<'inner, S, Typ = T>, ) -> Expr<'outer, S, i64>
Return the number of distinct values in a column.
for x in [-100, 10, 42, 10] {
txn.insert_ok(Val { x });
}
let (count1, count2) = txn.query_one(aggregate(|rows| {
let val = rows.join(Val);
let count1 = rows.count_distinct(&val.x);
rows.filter(false); // remove all rows
let count2 = rows.count_distinct(&val.x);
(count1, count2)
}));
assert_eq!(count1, 3);
assert_eq!(count2, 0);Sourcepub fn exists(&self) -> Expr<'outer, S, bool>
pub fn exists(&self) -> Expr<'outer, S, bool>
Return whether there are any rows.
for x in [10, 42, 10] {
txn.insert_ok(Val { x });
}
let (e1, e2) = txn.query_one(aggregate(|rows| {
rows.join(Val);
let e1 = rows.exists();
rows.filter(false); // removes all rows
let e2 = rows.exists();
(e1, e2)
}));
assert_eq!(e1, true);
assert_eq!(e2, false);Methods from Deref<Target = Rows<'inner, S>>§
Sourcepub fn join<T: DbTyp>(
&mut self,
j: impl IntoJoinable<'inner, S, Typ = T>,
) -> Expr<'inner, S, T>
pub fn join<T: DbTyp>( &mut self, j: impl IntoJoinable<'inner, S, Typ = T>, ) -> Expr<'inner, S, T>
Join a table, this is like a super simple Iterator::flat_map but for queries.
After this operation Rows has rows for the combinations of each original row with each row of the table. (Also called the “Carthesian product”) The expression that is returned refers to the joined table.
The parameter must be a table name from the schema like v0::User.
This table can be filtered by #[index]: rows.join(v0::User.score(100)).
See also Self::filter_some if you want to join a table that is filtered by #[unique].