cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Duplicate result

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 ?

1 ACCEPTED SOLUTION

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

View solution in original post

3 REPLIES 3

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

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

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