Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-19-2020 08:03 AM
Hi everyone;
I want to have all nodes A, but if more than one node A are linked to node B return only one:
MATCH(a:A)-[:R1]->(b:B) RETURN distinct a
.
Can anyone help me ?
Solved! Go to Solution.
05-19-2020 08:31 AM
Hi @multshibanda,
Here is my solution (there may be others):
MATCH(a:A)-[:R1]->(b:B)
WITH DISTINCT b AS b, collect(a) AS col
RETURN DISTINCT head(col)
The WITH
clause ensures that there is only one parent and collects all of its children and then returns the first child on the list for each parent:)
Regards,
Cobra
05-19-2020 08:09 AM
Hi multshibanda,
welcome to the Neo4j Community!
Do you want to return any single node A no matter which one or a special one?
If it is any - try:
MATCH(a:A)-[:R1]->(b:B) RETURN distinct a LIMIT 1
Regards,
Elena
05-19-2020 08:24 AM
Hi Elena, thank you for your response.
Let me give a case. suppose I have:
(Jhon)-[SON_OF]->(Gad)
(Bill)-[SON_OF]->(Gad)
(Yan)-[SON_OF]->(Karim)
(Herve)-[SON_OF]->(Gad)
I want this result :
Jhon
Yan
I do not want to have two or more children with the same parent
05-19-2020 08:31 AM
Hi @multshibanda,
Here is my solution (there may be others):
MATCH(a:A)-[:R1]->(b:B)
WITH DISTINCT b AS b, collect(a) AS col
RETURN DISTINCT head(col)
The WITH
clause ensures that there is only one parent and collects all of its children and then returns the first child on the list for each parent:)
Regards,
Cobra
All the sessions of the conference are now available online