cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

"Cypher intermediate queries - 5-pipelining queries - with scoping" special case.

The special case for the first WITH clause does not appear in the documentation.  e.g. (https://neo4j.com/docs/cypher-manual/current/clauses/with/) "Any variables not included in the WITH clause are not carried over to the rest of the query."  This statement seems counter to the fact that in the answer for "1. Scoping variables", the second WITH clause does not include the variables "a", and "t", but they are used in subsequent clauses.  This pattern also appears in the other examples on the GraphAcademy page.  Would it be correct to say, that if a WITH clause appears on the first line, those variables are considered global scope and any subsequent WITH clauses cannot remove those variables from the scope?

1 ACCEPTED SOLUTION

Hello @holleyism ,

Using WITH is sometimes tricky.
For this query:
WITH 'Clint Eastwood' AS a, 'high' AS t
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WITH p, m, toLower(m.title) AS movieTitle
WHERE p.name = a
AND movieTitle CONTAINS t
RETURN p.name AS actor, m.title AS movie

 

The first clause MATCH (p:Person)-[:ACTED_IN]->(m:Movie) has p, m, a, and t available to it.

The second clause 

WITH p, m, toLower(m.title) AS movieTitle
WHERE p.name = a
AND movieTitle CONTAINS t

has p, m. movieTitle, a, and t available to it

The third clause RETURN p.name AS actor, m.title AS movie has p and m available because they were defined in the previous WITH. Notice that a and t were not included with the second WITH so they are not available. With multiple clauses, there is no such thing as a global variable. Each clause has its set of available variables.

Elaine

View solution in original post

2 REPLIES 2

Hello @holleyism ,

Using WITH is sometimes tricky.
For this query:
WITH 'Clint Eastwood' AS a, 'high' AS t
MATCH (p:Person)-[:ACTED_IN]->(m:Movie)
WITH p, m, toLower(m.title) AS movieTitle
WHERE p.name = a
AND movieTitle CONTAINS t
RETURN p.name AS actor, m.title AS movie

 

The first clause MATCH (p:Person)-[:ACTED_IN]->(m:Movie) has p, m, a, and t available to it.

The second clause 

WITH p, m, toLower(m.title) AS movieTitle
WHERE p.name = a
AND movieTitle CONTAINS t

has p, m. movieTitle, a, and t available to it

The third clause RETURN p.name AS actor, m.title AS movie has p and m available because they were defined in the previous WITH. Notice that a and t were not included with the second WITH so they are not available. With multiple clauses, there is no such thing as a global variable. Each clause has its set of available variables.

Elaine

Ok.  I misread the query.  Thanks.