4.6. Aggregation

4.6.1. COUNT
4.6.2. Count nodes
4.6.3. Count entities
4.6.4. Count non null values
4.6.5. SUM
4.6.6. AVG
4.6.7. MAX
4.6.8. MIN

To calculate aggregated data, Cypher offers aggregation, much like SQL’s GROUP BY. If any aggregation functions are found in the RETURN statement, all the columns without aggregating functions are used as the grouping key.

4.6.1. COUNT

COUNT is used to count the number of rows. COUNT can be used in two forms - COUNT(*) which just counts the number of matching rows, and COUNT(<identifier>), which counts the number of non-null values in <identifier>.

4.6.2. Count nodes

To count the number of nodes, for example the number of nodes connected to one node, you can use count(*).

Query

start n=(2) match (n)-->(x) return n, count(*)

The start node and the count of related nodes.

Result

 +--------------------------------------------+
 | n                               | count(*) |
 +--------------------------------------------+
 | Node[2]{name->"A",property->13} | 3        |
 +--------------------------------------------+
 1 rows, 2 ms

4.6.3. Count entities

Instead of counting the number of results with count(*), it might be more expressive to include the name of the identifier you care about.

Query

start n=(2) match (n)-->(x) return count(x)

The number of connected nodes from the start node.

Result

 +----------+
 | count(x) |
 +----------+
 | 3        |
 +----------+
 1 rows, 2 ms

4.6.4. Count non null values

You can count the non-null values by using count(<identifier>).

Query

start n=(2,3,4,1) return count(n.property?)

The count of related nodes.

Result

 +-------------------+
 | count(n.property) |
 +-------------------+
 | 3                 |
 +-------------------+
 1 rows, 1 ms

4.6.5. SUM

The SUM aggregation function simply sums all the numeric values it encounters. Null values are silently dropped. This is an example of how you can use SUM.

Query

start n=(2,3,4) return sum(n.property)

The sum of all the values in the property property.

Result

 +-----------------+
 | sum(n.property) |
 +-----------------+
 | 90              |
 +-----------------+
 1 rows, 0 ms

4.6.6. AVG

AVG calculates the average of a numeric column.

Query

start n=(2,3,4) return avg(n.property)

The average of all the values in the property property.

Result

 +-----------------+
 | avg(n.property) |
 +-----------------+
 | 30.0            |
 +-----------------+
 1 rows, 1 ms

4.6.7. MAX

MAX find the largets value in a numeric column.

Query

start n=(2,3,4) return max(n.property)

The largest of all the values in the property property.

Result

 +-----------------+
 | max(n.property) |
 +-----------------+
 | 44              |
 +-----------------+
 1 rows, 0 ms

4.6.8. MIN

MIN takes a numeric property as input, and returns the smallest value in that column.

Query

start n=(2,3,4) return min(n.property)

The smallest of all the values in the property property.

Result

 +-----------------+
 | min(n.property) |
 +-----------------+
 | 13              |
 +-----------------+
 1 rows, 1 ms