Book Image

Learning Cypher

By : Onofrio Panzarino
Book Image

Learning Cypher

By: Onofrio Panzarino

Overview of this book

Table of Contents (13 chapters)

Aggregating results


In our application, we have users who rate books with a score from one to five. Now, we are going to query the database to get some aggregated information about book scores.

Counting matching rows or non-null values

Suppose that we want to know the number of users who have voted for a book. For this, we need to count the number of the vote relations between the users and that book, as shown in the following code snippet:

START b=node({id})
MATCH (b) <-[r:Vote]- (u:User)
RETURN COUNT(*) as votes

The only difference with the query patterns we already know is that here, we have used the COUNT function in the RETURN clause. With Cypher, the RETURN clause drives the aggregation of entities. In this case, as we have nothing else in the RETURN clause but the COUNT function, all the matching results are counted and the result is a single value. The result is as shown in the following output code:

+-------+
| votes |
+-------+
| 7     |
+-------+
1 row

The arguments inside the COUNT...