Ecto.Query.group_by
You're seeing just the macro
group_by
, go back to Ecto.Query module for more information.
A group by query expression.
Groups together rows from the schema that have the same values in the given
fields. Using group_by
"groups" the query giving it different semantics
in the select
expression. If a query is grouped, only fields that were
referenced in the group_by
can be used in the select
or if the field
is given as an argument to an aggregate function.
group_by
also accepts a list of atoms where each atom refers to
a field in source. For more complicated queries you can access fields
directly instead of atoms.
Keywords examples
# Returns the number of posts in each category
from(p in Post,
group_by: p.category,
select: {p.category, count(p.id)})
# Using atoms
from(p in Post, group_by: :category, select: {p.category, count(p.id)})
# Using direct fields access
from(p in Post,
join: c in assoc(p, :category),
group_by: [p.id, c.name])
Expressions example
Post |> group_by([p], p.category) |> select([p], count(p.id))