-
Notifications
You must be signed in to change notification settings - Fork 276
Neo4j::cypher aggregation
Read first how aggregation works here: http://docs.neo4j.org/chunked/1.8/query-aggregation.html
count
is used to count the number of rows. count
can be used in two forms — count()
with no arguments which just counts the number of matching rows, and count(<identifier>)
, which counts the number of non-null values in . Notice that the count
method is also available on path objects as well in the global context.
Example, count my friends
node(1).outgoing(:friends).count
Same as START v2=node(1) MATCH (v2)-[:`friends`]->(v1) RETURN count(v1)
The same could also be expressed like this:
node(1) >> :x
count(:x)
Use the global context version of count
for counting number of nodes connected to one node.
Example:
ret node(1).outgoing, count
Same as START v1=node(1) MATCH (v1)-->(v2) RETURN v2,count(*)
The sum
aggregation function simply sums all the numeric values it encounters. Nulls are silently dropped. This is an example of how you can use sum
Example:
node(2,3,4)[:value].sum
Generates: "START v1=node(2,3,4) RETURN sum(v1.value)"
avg
calculates the average of a numeric column.
Similar syntax to sum
, see above
Similar syntax to sum
, see above
Similar syntax to sum
, see above
collect
collects all the values into a list.
node(2,3,4)[:foo].collect
Generates "START v1=node(2,3,4) RETURN collect(v1.foo)
All aggregation functions also take the DISTINCT modifier, which removes duplicates from the values. So, to count the number of unique eye colors from nodes related to a, this query can be used:
Example, using a ret
block:
node(2) >> node(:b).ret{|n| n[:eyes].distinct}
Which generates: "START v1=node(2) MATCH (v1)-->(b) RETURN distinct(b.eyes)"
Or using separate lines:
node(2) >> :b
ret node(:b)[:eyes].distinct
WARNING: Much of the information in this wiki is out of date. We are in the process of moving things to readthedocs
- Project Introduction
- Neo4j::ActiveNode
- Neo4j::ActiveRel
- Search and Scope
- Validation, Uniqueness, and Case Sensitivity
- Indexing VS Legacy Indexing
- Optimized Methods
- Inheritance
- Core: Nodes & Rels
- Introduction
- Persistence
- Find : Lucene
- Relationships
- Third Party Gems & extensions
- Scaffolding & Generators
- HA Cluster