I have this kinds of relationships:
(:User)<-[:MENTIONS]-(:Tweet)-[:ABOUT]->(:Topic)
I would like to count all the users mentioned in the tweets regarding some topic.
With the following query
match (n:Topic)<--(t:Tweet)-[:MENTIONS]->(u:User)
where n.name='politics'
return distinct count(u)
all I get is the relationships count.
What I would like to get is, instead, the number of users mentioned (without duplicates if a user is mentioned several times). How is that possible?
Using count(*) to return the number of nodes The function count(*) can be used to return the number of nodes; for example, the number of nodes connected to some node n . The labels and age property of the start node n and the number of nodes related to n are returned.
DISTINCT retrieves only unique rows depending on the columns that have been selected to output. The node named "B" is returned by the query, but only once.
An OPTIONAL MATCH matches patterns against your graph database, just like a MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could be considered the Cypher equivalent of the outer join in SQL.
A relationship connects two nodes — a start node and an end node. Just like nodes, relationships can have properties. Relationships between nodes are a key part of a graph database.
Try putting distinct inside count
function, this way:
match (n:Topic)<-[:ABOUT]-(t:Tweet)-[:MENTIONS]->(u:User)
where n.name='politics'
return count(distinct u)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With