cancel
Showing results for 
Search instead for 
Did you mean: 

Return a COUNT VALUE from a SUB-QUERY and check if a condtion is satisfied

Hello everybody, I would like to create nested count structures and check if a certain condition(s) is (are) satisfied. To illustrated what I would like to achieve I have created a simple cypher-example as you see in the following query. However, this query is not working since after a CALL a WHERE cannot be placed. I also tried different constellations also inside the WHERE with EXISTS which is also not working.

 

MATCH (p:Regesta)

CALL { MATCH (p)-->(other) RETURN count(other) as myCount }

WHERE myCount > 3

RETURN p LIMIT 10

1 ACCEPTED SOLUTION

Hello @glilienfield,

thanks for your reply, but that is not what I wanted. I wanted to be flexible in the way I do the nesting with count. Since CALL {}-subquery can not be placed inside of a CALL {}-subquery, I assumed it should work with EXISTS{}-SUBQUERY. However, in EXISTS{}-SUBQUERY, no WITH-Clause can be called to define what shall be counted. CALL-IN-A-CALL still does not work see it at:

https://neo4j.com/docs/cypher-manual/5/clauses/call-subquery/
https://neo4j.com/docs/cypher-manual/4.4/clauses/call-subquery/

That is just regarding Cypher 4.4. I also asked my question in the discord developer chat, and just a few weeks later, they created Cypher 5.X functionality for subquery with Count. See: https://neo4j.com/docs/cypher-cheat-sheet/5/

From now on Cypher has the Subcall-procedure for COUNT.

MATCH (p:Regesta)
WHERE COUNT { (p)--(d:other) } > 1
RETURN p.name AS name

In this example the path will be counted and inside of the COUNT{} x further COUNTS{} can be placed.

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

Try this, as you don't need a subquery:

MATCH (p:Regesta)-->(other) 
WITH p, count(other) as myCount
WHERE myCount > 1
RETURN p, myCount
LIMIT 10

I refactored your query to get it to work:

MATCH (p:Regesta)
CALL { 
    WITH p
    MATCH (p)-->(other) 
    RETURN count(other) as myCount 
}
WITH p, myCount
WHERE myCount > 3
RETURN p, myCount
LIMIT 10

Hello @glilienfield,

thanks for your reply, but that is not what I wanted. I wanted to be flexible in the way I do the nesting with count. Since CALL {}-subquery can not be placed inside of a CALL {}-subquery, I assumed it should work with EXISTS{}-SUBQUERY. However, in EXISTS{}-SUBQUERY, no WITH-Clause can be called to define what shall be counted. CALL-IN-A-CALL still does not work see it at:

https://neo4j.com/docs/cypher-manual/5/clauses/call-subquery/
https://neo4j.com/docs/cypher-manual/4.4/clauses/call-subquery/

That is just regarding Cypher 4.4. I also asked my question in the discord developer chat, and just a few weeks later, they created Cypher 5.X functionality for subquery with Count. See: https://neo4j.com/docs/cypher-cheat-sheet/5/

From now on Cypher has the Subcall-procedure for COUNT.

MATCH (p:Regesta)
WHERE COUNT { (p)--(d:other) } > 1
RETURN p.name AS name

In this example the path will be counted and inside of the COUNT{} x further COUNTS{} can be placed.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.