Question

I have nodes labeled 'Group' and only some of them have relationships with people. T

GroupA {}, GroupB{personA, personB}, GroupC{}

I want to get all the groups and members that have groups assigned to them but I can only figure out to get groups with members

Match (:Client {value:'1234'})-[:IDENTIFIES]->(a)<-[r:PART_OF*]-(b:Group)
<-[:BELONGS_TO]-(per:Person)

Even if I put a separate Match with a UNION I'm not able to get both the empty groups and membered groups. It says column names have to be same. I'm probably doing something wrong.

Any help?? Thanks!!

Was it helpful?

Solution

This query should do what you want. The per value in a result row will be null if the b Group has no Person belonging to it. You can play around with this query using this console link.

MATCH (:Client {value:'1234'})-[:IDENTIFIES]->(a)<-[r:PART_OF*]-(b:Group)
OPTIONAL MATCH (b)<-[:BELONGS_TO]-(per:Person)
RETURN DISTINCT a, b, per;

OTHER TIPS

MATCH (c:Client {value: 1234})-[:IDENTIFIES]->(a)<-[r:PART_OF*]-(b:Group)<-[:BELONGS_TO]-             (per:Person), (g:Group) 
RETURN per, b, g 

This is assuming that you're ok with a duplication in the results in terms of the groups that are displayed. It also depends on how you want the results returned/displayed (e.g. you may want to use COLLECT). It might be a bit awkward to iterate through the "Member" results when each row contains all groups.

If you want just empty groups, maybe try something like:

MATCH (g:Group)
WHERE NOT g--()
RETURN g
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top