DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Trending

  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Zero Trust for AWS NLBs: Why It Matters and How to Do It
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  1. DZone
  2. Data Engineering
  3. Databases
  4. Neo4j/Cypher: SQL Style GROUP BY Functionality

Neo4j/Cypher: SQL Style GROUP BY Functionality

By 
Mark Needham user avatar
Mark Needham
·
Feb. 19, 13 · Tutorial
Likes (0)
Comment
Save
Tweet
Share
26.6K Views

Join the DZone community and get the full member experience.

Join For Free

As I mentioned in a previous post I’ve been playing around with some football related data over the last few days and one query I ran (using cypher) was to find all the players who’ve been sent off this season in the Premiership.

The model in the graph around sending offs looks like this:

Sending offs

My initial query looked like this:

START player = node:players('name:*')
MATCH player-[:sent_off_in]-game-[:in_month]-month
RETURN player.name, month.name

First we get the names of all the players which are stored in an index and then we follow relationships to the games they were sent off in and then find which months those games were played in.

That query returns:

+----------------------------+
| player.name  | month.name  |
+----------------------------+
| "Jenkinson"  | "February"  |
| "Chico"      | "September" |
| "Odemwingie" | "September" |
| "Agger"      | "August"    |
| "Cole"       | "December"  |
| "Whitehead"  | "August"    |
...
+----------------------------+

I thought it’d be interesting to see how many sending offs there were in each month which we’d achieve in SQL by making use of a GROUP BY.

cypher has a bunch of aggregation functions which allow us to achieve the same outcome.

In our case we want to use the COUNT function and we want our grouping key to be the month of the year so we need to include that as part of our RETURN statement as well:

START player = node:players('name:*')
MATCH player-[:sent_off_in]-game-[:in_month]-month
RETURN COUNT(player.name) AS numberOfReds, month.name
ORDER BY numberOfReds DESC

which returns:

+----------------------------+
| numberOfReds | month.name  |
+----------------------------+
| 7            | "October"   |
| 6            | "December"  |
| 4            | "September" |
| 4            | "November"  |
| 3            | "August"    |
| 2            | "January"   |
| 2            | "February"  |
+----------------------------+

As far as I can tell anything which isn’t an aggregate function is used as part of the grouping key which means we could include more than one field in our grouping key.

This isn’t particularly relevant for us for this particular query but would become useful if we add the teams that the players play for.

I extended the graph to included a player’s statistics for each game which also includes a relationship indicating which team they played for in a specific game.

The model now looks like this:

Stats

It does now look quite a bit more complicated but this was the best way I could think of modelling player specific details for a match.

I couldn’t see another way of modelling the fact that a player played for a certain team in a match which I want to use for some other queries but if you can see a simpler way please let me know.

To get a list of the red cards and the name of the team the offender played for we can write the following query:

START player = node:players('name:*')
MATCH player-[:sent_off_in]-game-[:in_month]-month, 
      game-[:in_match]-stats-[:stats]-player, 
      stats-[:played_for]-team
RETURN player.name, month.name, team.name
ORDER BY month.name

The original query traversed a path from a player to games they were sent off in and then from the games to the month the game was played in.

We’ve now added a traversal from the game to the game stats for that player and we also traverse from the game stats to the team node that the player played for in that game.

When we run this we get the following results:

+--------------------------------------------+
| player.name  | month.name  | team.name     |
+--------------------------------------------+
| "Agger"      | "August"    | "Liverpool"   |
| "Whitehead"  | "August"    | "Stoke"       |
...
| "Shotton"    | "December"  | "Stoke"       |
| "Nzonzi"     | "December"  | "Stoke"       |
| "Jenkinson"  | "February"  | "Arsenal"     |
...
| "Ivanovic"   | "October"   | "Chelsea"     |
| "Torres"     | "October"   | "Chelsea"     |
+--------------------------------------------+

So we can see that Stoke got 2 players sent off in December and Chelsea got 2 sent off in October.

We can write the following query to return a result set which uses team and month as the grouping key i.e. we count how many paths there are which have the same team and month:

START player = node:players('name:*')
MATCH player-[:sent_off_in]-game-[:in_month]-month, 
      game-[:in_match]-stats-[:stats]-player, 
      stats-[:played_for]-team
RETURN month.name, team.name, COUNT(player.name) AS numberOfReds
ORDER BY numberOfReds DESC

When we run that query we see the following results:

+--------------------------------------------+
| month.name  | team.name     | numberOfReds |
+--------------------------------------------+
| "December"  | "Stoke"       | 2            |
| "October"   | "Chelsea"     | 2            |
...
| "August"    | "Stoke"       | 1            |
| "November"  | "Tottenham"   | 1            |
| "December"  | "Everton"     | 1            |
+--------------------------------------------+

This is all explained in more detail in the documentation but I thought it’d be interesting to write about it from the perspective of someone more used to writing SQL and trying to work out how to achieve the same thing in cypher.



Database sql

Published at DZone with permission of Mark Needham, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!