cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Return distinct values from a merged collection

Hi, I'm working on a query to find probable siblings of a person in a family tree.

MATCH (p: Person) WHERE id(p) = 892 
OPTIONAL MATCH (p)-[:IS_CHILD_OF]->()<-[:IS_CHILD_OF]-(prop) 
OPTIONAL MATCH (p)-[:IS_SIBLING_OF]-(actualSibling)-[:IS_SIBLING_OF*1..4]-(prop2) 
RETURN DISTINCT collect(prop) + collect(prop2)

For this database state:
3X_c_0_c03ccc6ecb4c92cde18d7973f5d87518bdd93cc4.png

The query above executed for Harry returns William two times. How so? I think I used DISTINCT to make it return unique nodes. It seems that "prop" returns William twice, but if I had William in prop2, I would also want it to figure only once.

Could you explain why DISTINCT does not work and how could I return only unique nodes from this query?

1 ACCEPTED SOLUTION

I think I found the solution. Did not know that i could collect(distinct prop)...

MATCH (p: Person) WHERE id(p) = 892 
OPTIONAL MATCH (p)-[:IS_CHILD_OF]->()<-[:IS_CHILD_OF]-(prop) 
OPTIONAL MATCH (p)-[:IS_SIBLING_OF]-(actualSibling)-[:IS_SIBLING_OF*1..4]-(prop2) 
RETURN DISTINCT collect(distinct prop) + collect(distinct prop2)

View solution in original post

1 REPLY 1

I think I found the solution. Did not know that i could collect(distinct prop)...

MATCH (p: Person) WHERE id(p) = 892 
OPTIONAL MATCH (p)-[:IS_CHILD_OF]->()<-[:IS_CHILD_OF]-(prop) 
OPTIONAL MATCH (p)-[:IS_SIBLING_OF]-(actualSibling)-[:IS_SIBLING_OF*1..4]-(prop2) 
RETURN DISTINCT collect(distinct prop) + collect(distinct prop2)