sum

Function sum 

Source
pub fn sum<ST: Foldable, expr>(
    expr: expr,
) -> sum<ST, <expr as AsExpression<ST>>::Expression>
where expr: AsExpression<ST>,
Expand description

Represents a SQL SUM function. This function can only take types which are Foldable.

§Window Function Usage

This function can be used as window function. See WindowExpressionMethods for details

§Aggregate Function Expression

This function can be used as aggregate expression. See AggregateExpressionMethods for details.

§Examples

§Normal function usage

assert_eq!(Ok(Some(12i64)), animals.select(sum(legs)).first(connection));

§Window function

let res = animals
    .select((name, sum(legs).partition_by(id)))
    .load::<(Option<String>, Option<i64>)>(connection);

assert_eq!(
    Ok(vec![(Some("Jack".into()), Some(4)), (None, Some(8))]),
    res
);

§Aggregate function expression

assert_eq!(
    Ok(Some(4i64)),
    animals
        .select(sum(legs).aggregate_filter(legs.lt(8)))
        .first(connection)
);