Hello,
I assume my problem is not a new one, but I can't find an elegant solution.
merge (A:LEU {LEID: "A"})
merge (B:LEU {LEID: "B"})
merge (c:LEU {LEID: "C"})
merge (aa:LEU {LEID: "AA"})
WHERE (:LEU{LEID:"B"})<-[:CONTROLS0..]-(n) and (:LEU{LEID:"C"})<-[:CONTROLS0..]-(n)
RETURN collect(n)[0] as nn
My demand is about how to create dynamically the WHERE condition. I try the following, but it doesn't work. I assume that I must mix UNWIND, WHERE or FOREACH but how ?
MATCH (n:LEU)
WHERE EXISTS((n:LEU)<-[:CONTROLS]-(:LEU)) and NOT EXISTS((:LEU)<-[:CONTROLS]-(n:LEU))
WITH collect(n) as children
UNWIND children as child
MATCH (parent:LEU)
WHERE (child)<-[:CONTROLS*0..]-(parent)
RETURN parent
Query:
MATCH (b:LEU)-[:CONTROLS]->(n:LEU)
with collect(n) as nm
unwind nm as nm1
RETURN nm1
Result:
Query:
MATCH (b:LEU)-[:CONTROLS]->(n:LEU)
WHERE NOT EXISTS ((n)-[:CONTROLS]->(:LEU))
with collect(n) as nm
unwind nm as nm1
RETURN nm1
Result:
Query:
MATCH (n:LEU)
WHERE EXISTS ((:LEU)-[:CONTROLS]->(n))
AND NOT EXISTS ((n)-[:CONTROLS]->(:LEU))
WITH collect(n) as children
UNWIND children as child
MATCH (parent:LEU)
WHERE (child)<-[:CONTROLS*0..]-(parent)
RETURN parent
Here is my solution, one can takes directly the first common parent.
MATCH (n:LEU)
WHERE EXISTS((n:LEU)<-[:CONTROLS]-(:LEU)) and NOT EXISTS((:LEU)<-[:CONTROLS]-(n:LEU))
WITH collect(n) as children
MATCH (first_common_parent:LEU)
WHERE ALL(child in children WHERE ((child)<-[:CONTROLS*0..]-(first_common_parent)))
RETURN collect(first_common_parent)[0]
"Dynamically" means that the user mustn't enter manually the children.