cancel
Showing results for 
Search instead for 
Did you mean: 

Count the number of duplicates in result

ovashape
Node

Hello community,

I am new to neo4j and I'm trying to get a list of nodes (that has not a specific relation) and count node name duplicates.

Query:

MATCH (e:ExampleNode)
WHERE NOT exists((e)-[:EXAMPLE_REL]->())
RETURN e.name as name, count(e.name) as count

Result:

name count
name13
name231
name84

So far so good...

Beside the name of the node I also need some more values. This value makes each result unique and returns every node separately even (if the name is the same).

Query:

MATCH (e:ExampleNode)
WHERE NOT exists((e)-[:EXAMPLE_REL]->())
RETURN e.name as name, count(e.name) as count, e.property as someValue

Result:

name count someValue
name11abc
name11cba
name11xyz
name231abc
name81123
name81321
name81987
name81789
.........

But...this is the result I would like to have:

name count someValue
name13abc
name13cba
name13xyz
name231abc
name84123
name84321
name84987
name84789
.........

I would like to return every node that hasn't the specific relation and count all duplicates by the name.

So, is it possible to get the result I would like to have?

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @ovashape 🙂

MATCH (e:ExampleNode)
WHERE NOT exists((e)-[:EXAMPLE_REL]->())
WITH e.name as name, count(e.name) AS count, collect(e.property) AS values
UNWIND values AS value
RETURN name, count, value

Regards,

Cobra

View solution in original post

2 REPLIES 2

Cobra
Ninja
Ninja

Hello @ovashape 🙂

MATCH (e:ExampleNode)
WHERE NOT exists((e)-[:EXAMPLE_REL]->())
WITH e.name as name, count(e.name) AS count, collect(e.property) AS values
UNWIND values AS value
RETURN name, count, value

Regards,

Cobra

Thanks for your quick answer. This solved my problem.