Search code examples
neo4jcypher

Return count() from Cypher pattern comprehension


Is it possible to return count() from Cypher pattern comprehension ?

The following query doesn't work:

[ (:Vacancy)<-[rpp:POTENTIAL_PROFILE]-(childD) | count(rpp) ] AS potentialJobablesCount

with the following error:

Aggregation column contains implicit grouping expressions. For example, in 'RETURN n.a, n.a + n.b + count(*)' the aggregation expression 'n.a + n.b + count(*)' includes the implicit grouping key 'n.b'. It may be possible to rewrite the query by extracting these grouping/aggregation expressions into a preceding WITH clause. Illegal expression(s): rpp

Solution

  • Aggregation functions like COUNT can only be used in WITH and RETURN clauses.

    But starting with neo4j 5.0 you can use the new COUNT subquery. For example:

    MATCH (childD:ChildD {id:123})
    RETURN COUNT { (:Vacancy)<-[:POTENTIAL_PROFILE]-(childD) } AS potentialJobablesCount