Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
03-13-2022 06:30 AM
The following query returns the error: variable not defined.
MATCH (n:ID {start_marker: 1})
WITH DISTINCT collect(n.CASE_ID) AS start_marker
MATCH (m:ID)
WITH DISTINCT collect(m.CASE_ID) AS all
CALL apoc.coll.subtract(all, start_marker)
I understood that the with
keyword would pass the variable to the next statement, but I don't get it quite right.
How can I join two match
statements results and then compare them with e.g. some apoc function?
Solved! Go to Solution.
03-13-2022 12:25 PM
Your query has a few syntax errors in it. Try the following:
MATCH (n:ID {start_marker: 1})
WITH collect(DISTINCT n.CASE_ID) AS start_marker
MATCH (m:ID)
WITH start_marker, collect(DISTINCT m.CASE_ID) AS all
RETURN apoc.coll.subtract(all, start_marker)
In particular, the start_marker variable is not in scope in your query when it is referenced by the apoc function. You need to pass it in the 2nd WITH clause too. The apoc.coll.subtract is a function, so you don't use the CALL clause; you just call the function directly. Finally, the DISTINCT clause goes inside the collect method as part of its parameter. As a note, the apoc function seems to remove duplicates from the lists, so the DISTINCT clauses should not be necessary.
As an alternative approach, the following should provide the same result without depending on the apoc library:
MATCH (n:ID {start_marker: 1})
WITH collect(DISTINCT n.CASE_ID) AS start_marker
MATCH (m:ID)
WHERE NOT m.CASE_ID in start_marker
RETURN collect(DISTINCT m.CASE_ID) AS all
03-13-2022 10:21 AM
Hi,
Have you looked at the union function? Is it sufficient
Andy
03-13-2022 12:25 PM
Your query has a few syntax errors in it. Try the following:
MATCH (n:ID {start_marker: 1})
WITH collect(DISTINCT n.CASE_ID) AS start_marker
MATCH (m:ID)
WITH start_marker, collect(DISTINCT m.CASE_ID) AS all
RETURN apoc.coll.subtract(all, start_marker)
In particular, the start_marker variable is not in scope in your query when it is referenced by the apoc function. You need to pass it in the 2nd WITH clause too. The apoc.coll.subtract is a function, so you don't use the CALL clause; you just call the function directly. Finally, the DISTINCT clause goes inside the collect method as part of its parameter. As a note, the apoc function seems to remove duplicates from the lists, so the DISTINCT clauses should not be necessary.
As an alternative approach, the following should provide the same result without depending on the apoc library:
MATCH (n:ID {start_marker: 1})
WITH collect(DISTINCT n.CASE_ID) AS start_marker
MATCH (m:ID)
WHERE NOT m.CASE_ID in start_marker
RETURN collect(DISTINCT m.CASE_ID) AS all
All the sessions of the conference are now available online