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.
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>.
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
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
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
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
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
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
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
Copyright © 2011 Neo Technology