Aggregations
aggregate computes statistics over a set of records, and groupBy first groups records by fields and then computes per group.
aggregate
ts
const totals = await db.post.aggregate({
where: { published: true },
_count: true,
_sum: { views: true },
_avg: { views: true },
_min: { views: true },
_max: { views: true },
});1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
| Aggregation | Applicable fields | Empty dataset |
|---|---|---|
_count | Total records or non-null values of a non-JSON field | 0 |
_sum, _avg | Numeric fields | null |
_min, _max | Any field except JSON; boolean compares as false < true | null |
orderBy, skip, and take apply to the input records before aggregation. At least one aggregation must be selected.
Numeric Result Types
| Field type | _sum | _avg |
|---|---|---|
int, number | number | null | number | null |
bigint | string | null | string | null |
decimal | string | null | string | null |
String results avoid converting bigint and decimal aggregates to JavaScript numbers. Decimal having thresholds accept strings or finite numbers and are not limited by the source column's precision/scale.
groupBy
ts
const groups = await db.post.groupBy({
by: ["published"],
_count: { _all: true },
_sum: { views: true },
having: { views: { _sum: { gt: 10 } } },
orderBy: { _sum: { views: "desc" } },
take: 10,
});1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
bycontains at least one non-JSON scalar field and must not repeat a field; a database unique constraint is not required.- Plain fields used in
havingandorderBymust appear inby; aggregate expressions may use ungrouped fields. orderByis required wheneverskiportakeis used.havingnests at most 12 levels deep.
Next Steps
- Queries: filters and projections applied before aggregation
- Type Mapping: the physical type behind each numeric result