wdlax11
wdlax11

Reputation: 842

Neo4J - cypher query match differences

Hi guys I am trying to write a cypher query that will include 2 lists and I will then take the difference of them.

At the end of the day I need to actually run a MATCH query to get both lists and I can't figure out how to construct this to work.

So the two queries I need to run to generate the list are:

MATCH (Paper:Paper)<-[:WROTE]-(a:Author)
WHERE Paper.year > 2010 and Paper.year <2012
RETURN a

the 2nd one is the same query just a movement of the years from 2010 to 2011 and 2012 to 2013.

MATCH (Paper:Paper)<-[:WROTE]-(a:Author)
WHERE Paper.year > 2010 and Paper.year <2012
WITH a AS l1, 

(Paper:Paper)<-[:WROTE]-(a:Author)
WHERE Paper.year > 2011 and Paper.year <2013
WITH a AS l2

RETURN [Author in l2 WHERE not(Author in l1)] 

For some reason I get a skew of errors (not surprising) the one being Expression in WITH must be aliased (use AS) (line 5, column 1 (offset: 102)) "(Paper:Paper)<-[:WROTE]-(a:Author)"

any help would be appreciated!

Upvotes: 0

Views: 225

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

The reason for the error is you have an extra comma at the end of your first WITH clause.

That said, it would be wiser to collect the results rather than keep all result rows between your queries, since your current queries aren't working with your results as lists.

Some other improvements we can make include using chained inequalities:

MATCH (Paper:Paper)<-[:WROTE]-(a:Author)
WHERE 2010 < Paper.year < 2012
WITH collect(a) AS l1

MATCH (Paper:Paper)<-[:WROTE]-(a:Author)
WHERE 2011 < Paper.year < 2013
WITH l1, collect(a) AS l2

RETURN [Author in l2 WHERE not Author in l1] as authors

Upvotes: 1

Related Questions