Aggregate functions
Aggregates
Aggregate functions collapse many rows into one per group. The grouping key is
the set of non-aggregated items in the same RETURN/WITH.
| Aggregate | Description |
|---|---|
count(*) | Number of rows in the group |
count(x) | Number of rows where x is non-null |
collect(x) | Gather the non-null x values into a list |
sum(x) | Sum of numeric values |
avg(x) | Mean of numeric values |
min(x) | Minimum |
max(x) | Maximum |
MATCH (p:Person)
RETURN p.dept AS dept, count(*) AS n, avg(p.age) AS mean, collect(p.name) AS who
ORDER BY n DESC;
// count(*) on an empty group is 0 (one row is still produced):
MATCH (p:Person) WHERE false RETURN count(*) AS n; // 0
Aggregates nest with other expressions:
MATCH (p:Person) RETURN size(collect(p.name)) AS collected_rows;
MATCH (p:Person) RETURN count(*) + 1 AS n_plus_one;
Related articles
Indexes and constraints · Procedure directory · Transactions and concurrency