cancel
Showing results for 
Search instead for 
Did you mean: 

Return distinct values from a merged collection

damienthewave
Node Link

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

damienthewave
Node Link

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

damienthewave
Node Link

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)
Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.