Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-04-2021 09:03 AM
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:
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?
Solved! Go to Solution.
12-04-2021 10:48 AM
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)
12-04-2021 10:48 AM
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)
All the sessions of the conference are now available online