Hi!
I've got some problems to get the OPTIONAL MATCH to work when using the WITH and WHERE operators.
To simplify my complex query I have created the below example. I would like to return all Cats (a) and all persons (p) for whom all friends are bffs.
The problem is that if there is no such person then I will not get anything in result. I would still like the cat to be returned. I understand why it happens but how do I rewrite the query? Please don't just tell me to swap order of the two queries. ;-) Thanks!
Regards Ivar Hagen (Developer at Volvo Cars, Gothenburg, Sweden).
CREATE (a:Animal {type:'Cat', name:'Garfield'})
CREATE (p1:Person {name:'Abby'})
CREATE (p2:Person {name:'Betty'})
CREATE (p3:Person {name:'Carol'})
CREATE (p1)-[:HAS_FRIEND {type:'bff'}]->(p2)
CREATE (p1)-[:HAS_FRIEND {type:'bff'}]->(p3) // Removing this row and the query will return an empty set []
CREATE (p2)-[:HAS_FRIEND {type:'bff'}]->(p1)
CREATE (p2)-[:HAS_FRIEND]->(p3)
MATCH(a:Animal)
WHERE a.type = "Cat"
WITH COLLECT(a) AS a
OPTIONAL MATCH(p:Person)-[r2:HAS_FRIEND]->(p2:Person)
WITH a, p, COLLECT(p2) AS p2, COUNT(p2) AS friends, SIZE( (p)-[:HAS_FRIEND {type:"bff"}]->(:Person) ) AS bffs
WHERE friends = bffs
WITH a, p, p2
RETURN *