mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
doc/md: add example on how to group-by an edge
This commit is contained in:
committed by
Ariel Mashraki
parent
a51c50f6a3
commit
2bfb34fe82
@@ -50,3 +50,38 @@ func Do(ctx context.Context, client *ent.Client) {
|
||||
Strings(ctx)
|
||||
}
|
||||
```
|
||||
|
||||
## Group By Edge
|
||||
|
||||
Custom aggregation functions can be useful if you want to write your own storage-specific logic.
|
||||
|
||||
The following shows how to group by the `id` and the `name` of all users and calculate the average `age` of their pets.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"<project>/ent"
|
||||
"<project>/ent/pet"
|
||||
"<project>/ent/user"
|
||||
)
|
||||
|
||||
func Do(ctx context.Context, client *ent.Client) {
|
||||
var users []struct {
|
||||
ID int
|
||||
Name string
|
||||
Average float64
|
||||
}
|
||||
err := client.User.Query().
|
||||
GroupBy(user.FieldID, user.FieldName).
|
||||
Aggregate(func(s *sql.Selector) string {
|
||||
t := sql.Table(pet.Table)
|
||||
s.Join(t).On(s.C(user.FieldID), t.C(pet.OwnerColumn))
|
||||
return sql.As(sql.Avg(t.C(pet.FieldAge)), "average")
|
||||
}).
|
||||
Scan(ctx, &users)
|
||||
}
|
||||
```
|
||||
@@ -804,6 +804,8 @@ func Relation(t *testing.T, client *ent.Client) {
|
||||
Where(user.IDIn(foo.ID, bar.ID)).
|
||||
GroupBy(user.FieldID, user.FieldName).
|
||||
Aggregate(func(s *entsql.Selector) string {
|
||||
// Join with pet table and calculate the
|
||||
// average age of the pets of each user.
|
||||
t := entsql.Table(pet.Table)
|
||||
s.Join(t).On(s.C(user.FieldID), t.C(pet.OwnerColumn))
|
||||
return entsql.As(entsql.Avg(t.C(pet.FieldAge)), "average")
|
||||
|
||||
Reference in New Issue
Block a user