User12547645
User12547645

Reputation: 8497

neo4j: multiple values after with

I am getting started with neo4j and created a simple graph for practice. It contains students who are working in projects.

I would like to retrieve the students who are working in more than 2 projects and the respective projects, like this:

match (s:Student)-[w:WORKSON]->(p:Project) 
with s, p, count(w) as w_count 
where w_count > 2 return s, p

this gives me no result. However when I do this

match (s:Student)-[w:WORKSON]->(p:Project) 
with s, count(w) as w_count 
where w_count > 2 return s

i get the correct students and when I do this

match (s:Student)-[w:WORKSON]->(p:Project) 
with p, count(w) as w_count 
where w_count > 2 return p

I get the right projects. Glancing over the with documentation it says nothing about this case.

Upvotes: 0

Views: 67

Answers (1)

cybersam
cybersam

Reputation: 67044

You need to look at the documentation for aggregating functions (like COUNT), and how they work with grouping keys.

For example, in your first query:

match (s:Student)-[w:WORKSON]->(p:Project) 
with s, p, count(w) as w_count 
where w_count > 2 return s, p

the grouping keys are s and p, so COUNT(w) will only count how many WORKSON relationships exist between a given pair of s and p nodes (that have at least one such relationship between them). That count will be always 1, so the WHERE test always fails.

In order to "retrieve the students who are working in more than 2 projects and the respective projects", try this:

MATCH (s:Student)-[:WORKSON]->(p:Project) 
WITH s, COLLECT(p) AS ps
WHERE SIZE(ps) > 2
RETURN s, ps

Upvotes: 2

Related Questions