I am a newer to Cypher and I came across a problem that really bothered me. If I code like this
MATCH (r:Researcher)
OPTIONAL MATCH (r)-[:SUPERVISES]->(s:Student)
WITH r, count(s) AS studentsSupervised
MATCH (r)-[:AUTHORS]->(p1:Publication)
OPTIONAL MATCH (p1)<-[:CITES*]-(p2:Publication)
RETURN r.name, studentsSupervised, count(DISTINCT p2) AS citedCount`
It will return the correct answer. But if I do like that
MATCH (r:Researcher)
MATCH (r)-[:AUTHORS]->(p1:Publication)
OPTIONAL MATCH (r)-[:SUPERVISES]->(s:Student)
OPTIONAL MATCH (p1)<-[:CITES*]-(p2:Publication)
WITH r, count(s) AS studentsSupervised
RETURN r.name, studentsSupervised, count(DISTINCT p2) AS citedCount
I got a Error Neo.ClientError.Statement.SyntaxError which referred to Variable p2 not defined (line6, column 51) in "RETURN r.name, students Supervised, count(DISTINCT p2) AS citedCount"
I am puzzled about it and it seems WITH cuts the relation in the context.
I will appreciate it if someone knows, please let me know the internal reasons!!!