Count distinct value for a relationship property

Hi all,

why these two Cypher statements returns different results?

MATCH ()-[r:REL]->() RETURN, count(*)
MATCH ()-[r:REL]-() RETURN, count(*)

The first cypher statement use a directed relationship, this is the only difference.
nodeA - rel1 -> nodeB is counted twice?
Even if nodeB - rel2 -> nodeA exists?


couple of issues here.

  • the title/subject speaks of count distinct but I'm not seeing a distinct in the example queries

  • though I dont suspect a bug but without a Neo4j version... ????

  • you can reproduce this with and with Neo4j 4.2.2

@relcount> create (a)-[:REL {id:1}]->(b);
0 rows available after 301 ms, consumed after another 0 ms
Added 2 nodes, Created 1 relationships, Set 1 properties
@relcount> MATCH ()-[r:REL]->() RETURN, count(*)
| | count(*) |
| 1    | 1        |

1 row available after 482 ms, consumed after another 119 ms
@relcount> MATCH ()-[r:REL]-() RETURN, count(*)
| | count(*) |
| 1    | 2        |

1 row available after 242 ms, consumed after another 8 ms

in my first create node a has a outgoing relationship to node b.

the first match indicates find any node which has a outgoing relationship, which has a relationship type of REL, to some other node. in this case the only node which satisfies this is node a

the 2nd match find any node which has a relationship, which has a relationship type of REL to some other node, regardless of direction. In this case node a is eligible as it has a outgoing REL relationship to b and b is also elgible as it has a incoming REL relationship to a and thus a count of 2

Thank you Dana!

I'm sorry for the misleading title...even if it was exactly what I want to do...just count the distinct values of a relationship property :-)

According to what you wrote it seems that all relationship are counted twice in an undirected pattern, isn't so?
This is not surprising even if it could seem a little bit weird

I write here because, in my db, the results of the second query are different from the results of the first query multiplied by 2...

Unfortunatly I can't reproduce those result 'cause the db is updated just now.

Anyway, thank you