How to find the closest common parent?

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"})

merge (a)-[:CONTROLS {share:toFloat(100.0)}]->(b)
merge (a)-[:CONTROLS {share:toFloat(100.0)}]->(c)
merge (aa)-[:CONTROLS {share:toFloat(100.0)}]->(a)

I run the following code, it works.

MATCH (n:LEU)

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

Hi @depire,

Please explain what do you mean by dynamic Where ..
If you tells us exactly what the requirement is then group can help you much better way

Here is your starting scenario:

Screen Shot 2020-04-15 at 11.56.39 PM

Query:
MATCH (b:LEU)-[:CONTROLS]->(n:LEU)
with collect(n) as nm
unwind nm as nm1
RETURN nm1

Result:

Screen Shot 2020-04-15 at 11.57.56 PM

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:

Screen Shot 2020-04-15 at 11.58.34 PM

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

Result:

Hope this helps.

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.

That may be could be useful for somelse.

That's really great that you solved the problem by yourself :metal:.
Please mark the post as solution so that others could use it

match (a:LEU)-[r:CONTROLS]-(b:LEU)
with a, count(r) as cnt where cnt > 1
return a

This returns node "A"