Data aggregation in Cypher works similarly to aggregation in SQL, with a variety of functions that allow you to group and summarize your data. In this section, we’ll cover how to collect values, count data, and use other aggregation functions.
Collecting Values
The COLLECT function is used to create a list of values. It can be useful when you want to aggregate nodes, relationships, or properties into a list.
MATCH (p:Person)
RETURN p.name, COLLECT(p.age) AS ages
This query returns each person’s name along with a list of their ages. If there were multiple Person nodes with the same name, it would collect all of their ages into one list.
collect() skips nulls and returns an empty list when it has no values to collect. Use collect(DISTINCT value) to keep one copy of each value. Use WITH … ORDER BY to order the input to collect(). This also works for paths.
MATCH p = (:Person {name: 'Alice'})-[:KNOWS*]->(:Person)
WITH p ORDER BY length(p)
RETURN collect(DISTINCT p) AS paths
Counting
The COUNT function is used to count the number of rows, nodes, relationships, or properties that match a given pattern.
MATCH (p:Person)
RETURN COUNT(p)
This query counts the number of Person nodes in the database. count(*) counts input rows, including optional rows with null values. count(value) counts rows for which value is non-null.
To count distinct values, use COUNT(DISTINCT ...). For example, to count the number of distinct ages among Person nodes:
MATCH (p:Person)
RETURN COUNT(DISTINCT p.age)
Grouping and DISTINCT compare numeric values by value, so 1 and 1.0 belong to the same group. The same comparison applies within lists and maps, with map keys retained. Nodes and relationships group by identity. Paths group by their ordered nodes and relationships.
Other Aggregation Functions
Cypher includes a variety of other aggregation functions for summarizing your data:
SUM: Calculates the sum of numeric values.MATCH (p:Person) RETURN SUM(p.age)AVG: Calculates the average of numeric values.MATCH (p:Person) RETURN AVG(p.age)MINandMAX: Find the minimum or maximum of ordered values, including numbers, temporal values, strings, and lists. Lists are compared element by element.MATCH (p:Person) RETURN MIN(p.age), MAX(p.age)stDev(value)calculates the sample standard deviation.stDevP(value)calculates the population standard deviation.
UNWIND [1, 2, 3] AS value
RETURN stDev(value) AS sample_deviation, stDevP(value) AS population_deviation
The sample deviation is 1.0 and the population deviation is about 0.8165.
Percentiles
percentileDisc(value, fraction) selects a value from the ordered inputs. percentileCont(value, fraction) interpolates between values. Use a fraction from 0 through 1, inclusive. Both functions skip null inputs and return null when no non-null values remain. Use DISTINCT before the value expression to discard duplicate values.
UNWIND [4, 1, 2, 3, null] AS n
RETURN percentileDisc(n, 0.5) AS p50Discrete,
percentileCont(n, 0.5) AS p50Continuous
The values are 2 and 2.5.
Grouping and Aggregate Expressions
In an aggregate projection, expressions without an aggregate define the grouping keys. Each aggregate operates on the rows in its group.
For example, to get the average age of Person nodes, grouped by their name:
MATCH (p:Person)
RETURN p.name, AVG(p.age)
This query returns each distinct name with its average age. When there are no non-null inputs, sum() returns zero and avg(), min(), and max() return null.
An expression can combine aggregate results with literals and simple grouping keys, such as a variable or a property access projected as a grouping key.
UNWIND [1, 1, 2] AS n
RETURN n, n + count(*) * 10 + sum(n) AS score
ORDER BY score
This returns (2, 14) and (1, 23). To use a compound grouping key inside an aggregate expression, bind it to a variable in a preceding WITH. Aggregate calls cannot be nested. To aggregate sampled values from rand(), project the sample in a preceding WITH and aggregate the alias.
UNWIND [1, 1, 2] AS n
WITH n + 1 AS bucket
RETURN bucket, bucket + count(*) AS score
ORDER BY bucket
This returns (2, 4) and (3, 4).
ORDER BY can use grouping aliases and aggregate results already computed by the projection. An aggregate used only in ORDER BY is invalid. Use an alias for a compound grouping key, as in the following query.
MATCH p = (:Person)-[:KNOWS*]->(:Person)
RETURN length(p) AS hops, count(*) AS paths
ORDER BY hops
With zero input rows, aggregation without grouping keys still produces one row. For example, the following query returns 0 and [] if there are no Person nodes. Adding p.name as a grouping key would yield zero rows in that case.
MATCH (p:Person)
RETURN count(*) AS people, collect(p.age) AS ages
Understanding how to aggregate data will allow you to write more powerful and flexible queries, enabling you to derive insights from your graph data in Polypheny.