Groupby operation
Genreally, groupby is used to group a set of entities based on one or more of their properties and aggregate some other (one) property within this group.
A simple example is count
of people from different countries.
{
me(func: gt(count(country), 1)) @groupby(country) {
count( _uid_ )
}
}
Whatever is inside the @groupby level can only be aggregates.
Note: The argument to groupby can be values or ids
If we want to group by last name
and country
{
me(func: gt(count(country), 1)) @groupby(country, last.name) {
count( _uid_ )
}
}
The output of this groupby would contain the fields supplied to groupby and the aggregate value. (so we find the unique countries and lastnames and for each combination of them, count the number of people)
{
me: [{"lastname": "alice", "country": "XYZ", "count(_uid_)": 5}]
}
# to select the max dob among my friends with same name
{
me(id: a) {
friends @groupby(name) {
max(dob)
}
}
}
# Find the number of films of a director each actor has acted in.
# (Useful to find the most frequent actor.
{
director(id: a) {
films @groupby(actorid) {
count(_uid_)
}
}
}
}
# Find the latest film of all the actors who have acted with a director.
{
director(id: a) {
films @groupby(actorid) {
max(initial_release_date)
}
}
}
}
Please do leave your thoughts on this design, use-cases you might have for groupby
and any modifications youâd like to see.