Aggregating functions
Aggregating functions take a set of values and calculate an aggregated value over them.
Functions:
Aggregation can be computed over all the matching paths, or it can be further divided by introducing grouping keys. Grouping keys are nonaggregate expressions, that are used to group the values going into the aggregate functions.
Assume we have the following return statement:
RETURN n, count(*)
We have two return expressions: n
, and count(*)
.
The first, n
, is not an aggregate function, so it will be the grouping key.
The latter, count(*)
is an aggregate expression.
The matching paths will be divided into different buckets, depending on the grouping key.
The aggregate function will then be run on these buckets, calculating an aggregate value per bucket.
The input expression of an aggregation function can contain any expression, including expressions that are not grouping keys.
However, not all expressions can be composed with aggregation functions.
The example below will throw an error since we compose n.x
, which is not a grouping key, with the aggregation expression count(*)
.
For more information see Grouping keys.
RETURN n.x + count(*)
To use aggregations to sort the result set, the aggregation must be included in the RETURN
to be used in the ORDER BY
.
The DISTINCT
operator works in conjunction with aggregation.
It is used to make all values unique before running them through an aggregate function.
More information about DISTINCT
may be found in Syntax → Aggregation operators.
The following graph is used for the examples below:
avg()  Numeric values
The function avg()
returns the average of a set of numeric values.
Syntax:
avg(expression)
Returns:
Either an Integer or a Float, depending on the values returned by 
Arguments:
Name  Description 


An expression returning a set of numeric values. 
Considerations:
Any 

MATCH (n:Person)
RETURN avg(n.age)
The average of all the values in the property age
is returned.
avg(n.age) 


Rows: 1 
avg()  Durations
The function avg()
returns the average of a set of Durations.
Syntax:
avg(expression)
Returns:
A Duration. 
Arguments:
Name  Description 


An expression returning a set of Durations. 
Considerations:
Any 

UNWIND [duration('P2DT3H'), duration('PT1H45S')] AS dur
RETURN avg(dur)
The average of the two supplied Durations is returned.
avg(dur) 


Rows: 1 
collect()
The function collect()
returns a single aggregated list containing the values returned by an expression.
Syntax:
collect(expression)
Returns:
A list containing heterogeneous elements; the types of the elements are determined by the values returned by 
Arguments:
Name  Description 


An expression returning a set of values. 
Considerations:
Any 

MATCH (n:Person)
RETURN collect(n.age)
All the values are collected and returned in a single list.
collect(n.age) 


Rows: 1 
count()
The function count()
returns the number of values or rows, and appears in two variants:
count(*)

returns the number of matching rows.
count(expr)

returns the number of non
null
values returned by an expression.
Syntax:
count(expression)
Returns:
An Integer. 
Arguments:
Name  Description 


An expression. 
Considerations:



Using count(*)
to return the number of nodes
The function count(*)
can be used to return the number of nodes; for example, the number of nodes connected to some node n
.
MATCH (n {name: 'A'})>(x)
RETURN labels(n), n.age, count(*)
The labels and age
property of the start node n
and the number of nodes related to n
are returned.
labels(n)  n.age  count(*) 




Rows: 1 
Using count(*)
to group and count relationship types
The function count(*)
can be used to group the type of matched relationships and return the number.
MATCH (n {name: 'A'})[r]>()
RETURN type(r), count(*)
The type of matched relationships are grouped and the group count are returned.
type(r)  count(*) 





Rows: 2 
Counting nonnull
values
Instead of simply returning the number of rows with count(*)
, the function count(expression)
can be used to return the number of nonnull
values returned by the expression.
MATCH (n:Person)
RETURN count(n.age)
The number of nodes with the label Person
and a property age
is returned. (If you want the sum, use sum(n.age)
)
count(n.age) 


Rows: 1 
Counting with and without duplicates
In this example we are trying to find all our friends of friends, and count them:
count(DISTINCT friend_of_friend)

Will only count a
friend_of_friend
once, asDISTINCT
removes the duplicates. count(friend_of_friend)

Will consider the same
friend_of_friend
multiple times.
MATCH (me:Person)>(friend:Person)>(friend_of_friend:Person)
WHERE me.name = 'A'
RETURN count(DISTINCT friend_of_friend), count(friend_of_friend)
Both B
and C
know D
and thus D
will get counted twice when not using DISTINCT
.
count(DISTINCT friend_of_friend)  count(friend_of_friend) 



Rows: 1 
max()
The function max()
returns the maximum value in a set of values.
Syntax:
max(expression)
Returns:
A property type, or a list, depending on the values returned by 
Arguments:
Name  Description 


An expression returning a set containing any combination of property types and lists thereof. 
Considerations:
Any 
In a mixed set, any numeric value is always considered to be higher than any string value, and any string value is always considered to be higher than any list. 
Lists are compared in dictionary order, i.e. list elements are compared pairwise in ascending order from the start of the list to the end. 

UNWIND [1, 'a', null, 0.2, 'b', '1', '99'] AS val
RETURN max(val)
The highest of all the values in the mixed set — in this case, the numeric value 1
— is returned.
The value 
max(val) 


Rows: 1 
UNWIND [[1, 'a', 89], [1, 2]] AS val
RETURN max(val)
The highest of all the lists in the set — in this case, the list [1, 2]
— is returned, as the number 2
is considered to be a higher value than the string 'a'
, even though the list [1, 'a', 89]
contains more elements.
max(val) 


Rows: 1 
MATCH (n:Person)
RETURN max(n.age)
The highest of all the values in the property age
is returned.
max(n.age) 


Rows: 1 
min()
The function min()
returns the minimum value in a set of values.
Syntax:
min(expression)
Returns:
A property type, or a list, depending on the values returned by 
Arguments:
Name  Description 


An expression returning a set containing any combination of property types and lists thereof. 
Considerations:
Any 
In a mixed set, any string value is always considered to be lower than any numeric value, and any list is always considered to be lower than any string. 
Lists are compared in dictionary order, i.e. list elements are compared pairwise in ascending order from the start of the list to the end. 

UNWIND [1, 'a', null, 0.2, 'b', '1', '99'] AS val
RETURN min(val)
The lowest of all the values in the mixed set — in this case, the string value "1"
— is returned.
Note that the (numeric) value 0.2
, which may appear at first glance to be the lowest value in the list, is considered to be a higher value than "1"
as the latter is a string.
min(val) 


Rows: 1 
UNWIND ['d', [1, 2], ['a', 'c', 23]] AS val
RETURN min(val)
The lowest of all the values in the set — in this case, the list ['a', 'c', 23]
— is returned, as (i) the two lists are considered to be lower values than the string "d"
, and (ii) the string "a"
is considered to be a lower value than the numerical value 1
.
min(val) 


Rows: 1 
MATCH (n:Person)
RETURN min(n.age)
The lowest of all the values in the property age
is returned.
min(n.age) 


Rows: 1 
percentileCont()
The function percentileCont()
returns the percentile of the given value over a group, with a percentile from 0.0
to 1.0
.
It uses a linear interpolation method, calculating a weighted average between two values if the desired percentile lies between them.
For nearest values using a rounding method, see percentileDisc
.
Syntax:
percentileCont(expression, percentile)
Returns:
A Float. 
Arguments:
Name  Description 


A numeric expression. 

A numeric value between 
Considerations:
Any 

MATCH (n:Person)
RETURN percentileCont(n.age, 0.4)
The 40th percentile of the values in the property age
is returned, calculated with a weighted average.
percentileCont(n.age, 0.4) 


Rows: 1 
percentileDisc()
The function percentileDisc()
returns the percentile of the given value over a group, with a percentile from 0.0
to 1.0
.
It uses a rounding method and calculates the nearest value to the percentile.
For interpolated values, see percentileCont
.
Syntax:
percentileDisc(expression, percentile)
Returns:
Either an Integer or a Float, depending on the values returned by 
Arguments:
Name  Description 


A numeric expression. 

A numeric value between 
Considerations:
Any 

MATCH (n:Person)
RETURN percentileDisc(n.age, 0.5)
The 50th percentile of the values in the property age
is returned.
percentileDisc(n.age, 0.5) 


Rows: 1 
stDev()
The function stDev()
returns the standard deviation for the given value over a group.
It uses a standard twopass method, with N  1
as the denominator, and should be used when taking a sample of the population for an unbiased estimate.
When the standard variation of the entire population is being calculated, stdDevP
should be used.
Syntax:
stDev(expression)
Returns:
A Float. 
Arguments:
Name  Description 


A numeric expression. 
Considerations:
Any 

MATCH (n)
WHERE n.name IN ['A', 'B', 'C']
RETURN stDev(n.age)
The standard deviation of the values in the property age
is returned.
stDev(n.age) 


Rows: 1 
stDevP()
The function stDevP()
returns the standard deviation for the given value over a group.
It uses a standard twopass method, with N
as the denominator, and should be used when calculating the standard deviation for an entire population.
When the standard variation of only a sample of the population is being calculated, stDev
should be used.
Syntax:
stDevP(expression)
Returns:
A Float. 
Arguments:
Name  Description 


A numeric expression. 
Considerations:
Any 

MATCH (n)
WHERE n.name IN ['A', 'B', 'C']
RETURN stDevP(n.age)
The population standard deviation of the values in the property age
is returned.
stDevP(n.age) 


Rows: 1 
sum()  Numeric values
The function sum()
returns the sum of a set of numeric values.
Syntax:
sum(expression)
Returns:
Either an Integer or a Float, depending on the values returned by 
Arguments:
Name  Description 


An expression returning a set of numeric values. 
Considerations:
Any 

MATCH (n:Person)
RETURN sum(n.age)
The sum of all the values in the property age
is returned.
sum(n.age) 


Rows: 1 
sum()  Durations
The function sum()
returns the sum of a set of durations.
Syntax:
sum(expression)
Returns:
A Duration. 
Arguments:
Name  Description 


An expression returning a set of Durations. 
Considerations:
Any 
UNWIND [duration('P2DT3H'), duration('PT1H45S')] AS dur
RETURN sum(dur)
The sum of the two supplied Durations is returned.
sum(dur) 


Rows: 1 
Grouping keys
Aggregation expressions are expressions which contain one or more aggregation functions.
A simple aggregation expression consists of a single aggregation function.
For instance, SUM(x.a)
is an aggregation expression that only consists of the aggregation function SUM( )
with x.a
as its argument.
Aggregation expressions are also allowed to be more complex, where the result of one or more aggregation functions
are input arguments to other expressions.
For instance, 0.1 * (SUM(x.a) / COUNT(x.b))
is an aggregation expression that contains two aggregation functions,
SUM( )
with x.a
as its argument and COUNT( )
with x.b
as its argument.
Both are input arguments to the division expression.
For aggregation expressions to be correctly computable for the buckets formed by the grouping key(s), they have to fulfill some requirements. Specifically, each sub expression in an aggregation expression has to be either:

an aggregation function, e.g.
SUM(x.a)
, 
a constant, e.g.
0.1
, 
a parameter, e.g.
$param
, 
a grouping key, e.g. the
a
inRETURN a, count(*)

a local variable, e.g. the
x
incount(*) + size([ x IN range(1, 10)  x ])
, or 
a subexpression, all whose operands are operands allowed in an aggregation expression.
Examples of aggregation expressions.
MATCH (p: Person) RETURN max(p.age)
max(p.age) 


Rows: 1 
MATCH (p: Person) RETURN max(p.age) + 1
max(p.age) + 1 


Rows: 1 
Note that n
is a grouping key:
MATCH (n: Person{name:"A"})[:KNOWS](f:Person) RETURN n, n.age  max(f.age)
n  n.age  max(f.age) 



Rows: 1 
Note that n.age
is a grouping key:
MATCH (n: Person{name:"A"})[:KNOWS](f:Person) RETURN n.age, n.age  max(f.age)
n.age  n.age  max(f.age) 



Rows: 1 
Grouping keys themselves can be complex expressions. For better query readability, Cypher only recognizes a subexpression in aggregation expressions as a grouping key if the grouping key is either:

A variable  e.g. the
n
inRETURN n, n.age  max(f.age)

A property access  e.g. the
n.age
inRETURN n.age, n.age  max(f.age)

A map access  e.g. the
n.age
inWITH {age: 34, name:Chris} AS n RETURN n.age, n.age  max(n.age)
If more complex grouping keys are needed as operands in aggregation expression, it is always possible to project them in advance with WITH
.
Using the property n.age
will throw an exception, since n.age
is not a grouping key:
MATCH (n: Person{name:"A"})[:KNOWS](f:Person) RETURN n.age  max(f.age)
n.age + n.age
is not a valid grouping key, since the expression is not a variable, property access or map access. It can therefore not be used in the expression which contains the aggregation function:
MATCH (n: Person{name:"A"})[:KNOWS](f:Person) RETURN n.age + n.age, n.age + n.age  max(f.age)
The above query could be rewritten to:
MATCH (n: Person{name:"A"})[:KNOWS](f:Person) WITH n.age + n.age AS groupingKey, f RETURN groupingKey, groupingKey  max(f.age)
Was this page helpful?