Skip to main content

Aggregate Functions

You can group results selected from SeaORM find with the group_by method, and if you wish to further restrict the grouped result set, the having method can help you achieve that.

Group By#

The group_by method can take a column of the entity or a complex sea_query::SimpleExpr.

assert_eq!(    cake::Entity::find()        .select_only()        .column(cake::Column::Name)        .group_by(cake::Column::Name)        .build(DbBackend::Postgres)        .to_string(),    r#"SELECT "cake"."name" FROM "cake" GROUP BY "cake"."name""#);

Having#

The having method can take any conditional expressions introduced in the previous section.

assert_eq!(    cake::Entity::find()        .having(cake::Column::Id.eq(4))        .having(cake::Column::Id.eq(5))        .build(DbBackend::MySql)        .to_string(),    "SELECT `cake`.`id`, `cake`.`name` FROM `cake` HAVING `cake`.`id` = 4 AND `cake`.`id` = 5");