Ecto.Repo.aggregate
You're seeing just the callback
aggregate
, go back to Ecto.Repo module for more information.
Specs
aggregate( queryable :: Ecto.Queryable.t(), aggregate :: :count, opts :: Keyword.t() ) :: term() | nil
Calculate the given aggregate
.
If the query has a limit, offset or distinct set, it will be automatically wrapped in a subquery in order to return the proper result.
Any preload or select in the query will be ignored in favor of the column being aggregated.
The aggregation will fail if any group_by
field is set.
Options
:prefix
- The prefix to run the query on (such as the schema path in Postgres or the database in MySQL). This will be applied to allfrom
andjoin
s in the query that did not have a prefix previously given either via the:prefix
option onjoin
/from
or via@schema_prefix
in the schema. For more information see the "Query Prefix" section of theEcto.Query
documentation.
See the "Shared options" section at the module documentation for more options.
Examples
# Returns the number of blog posts
Repo.aggregate(Post, :count)
# Returns the number of blog posts in the "private" schema path
# (in Postgres) or database (in MySQL)
Repo.aggregate(Post, :count, prefix: "private")
Specs
aggregate( queryable :: Ecto.Queryable.t(), aggregate :: :avg | :count | :max | :min | :sum, field :: atom(), opts :: Keyword.t() ) :: term() | nil
Calculate the given aggregate
over the given field
.
See aggregate/3
for general considerations and options.
Examples
# Returns the number of visits per blog post
Repo.aggregate(Post, :count, :visits)
# Returns the number of visits per blog post in the "private" schema path
# (in Postgres) or database (in MySQL)
Repo.aggregate(Post, :count, :visits, prefix: "private")
# Returns the average number of visits for the top 10
query = from Post, limit: 10
Repo.aggregate(query, :avg, :visits)