Files
ent/doc/md/aggregate.md
Ariel Mashraki 78c58190be ent/doc: traversal and aggregation and predicates
Reviewed By: alexsn

Differential Revision: D17113889

fbshipit-source-id: edf4d9af9660fe31e0d02f58a65bcbc4d549a695
2019-08-29 09:01:33 -07:00

52 lines
785 B
Markdown
Executable File

---
id: aggregate
title: Aggregation
---
## Group By
Group by `name` and `age` fields of all users, and sum their total age.
```go
package main
import (
"context"
"<project>/ent"
"<project>/ent/user"
)
func Do(ctx context.Context, client *ent.Client) {
var v []struct {
Name string `json:"name"`
Age int `json:"age"`
Sum int `json:"sum"`
Count int `json:"count"`
}
err := client.User.Query().
GroupBy(user.FieldName, user.FieldAge).
Aggregate(ent.Count(), ent.Sum(user.FieldAge)).
Scan(ctx, &v)
}
```
Group by one field.
```go
package main
import (
"context"
"<project>/ent"
"<project>/ent/user"
)
func Do(ctx context.Context, client *ent.Client) {
names, err := client.User.
Query().
GroupBy(user.FieldName).
Strings(ctx)
}
```