Book Image

Neo4j Cookbook

By : Ankur goel, Ankur Goel
Book Image

Neo4j Cookbook

By: Ankur goel, Ankur Goel

Overview of this book

Table of Contents (17 chapters)
Neo4j Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Aggregation with Cypher


The Cypher query offers aggregation similar to GROUP BY offered by SQL. The aggregate function can take multiple values and can calculate the aggregated values for them. In this recipe, we will learn the common aggregation techniques, with the help of examples.

Getting ready

To work through this recipe, you will need to create the nodes and relationships for which data has been provided with the code files.

How to do it...

We have divided this recipe into the following problem sets:

  • Nodes and relationships: Let's get all the nodes and then count the number of relationships they have with the other nodes:

    START n = node(*) 
    MATCH n-[r]-() RETURN n, count(r) as rel_count ORDER BY rel_count desc

    The following screenshot shows the Neo4j console depicting nodes and count of relationships they share with the other nodes in the graph:

  • Count the number of entities: Let's count the total number of flights originating from any airport:

    START n = node(*)MATCH (n)-[r:`ORIGIN`]->(o...