liveBook · Manning

Grouping key

As explained in Graph Algorithms for Data Science

An implicit grouping key is a non-aggregated query column that determines how rows are grouped when an aggregation is evaluated.

A grouping key is a non-aggregated expression that determines which rows contribute to each aggregate result. In Cypher, grouping keys are implicit: once a query uses an aggregation function, every non-aggregated value in its WITH or RETURN clause becomes a grouping key. section 4.2

Use grouping keys to organize aggregate results around graph attributes:

  • Count nodes by label with labels(n) alongside count(n); the returned labels value becomes the grouping key. section 4.2
  • Count tweets by year by returning the extracted year with count; add both year and month to group by month within a year. section 4.2.1
  • Use non-aggregate values in WITH or RETURN instead of adding a separate GROUP BY clause. section 4.2

Counting tweets by year and month

A query returning t.createdAt.year, t.createdAt.month, and count(*) groups tweets by each year-month pair. The resulting distribution shows 8,507 tweets in May 2021 and 2,695 in June 2021. section 4.2.1

Listing 4.5 Counting the number of nodes by labels
MATCH (n)
RETURN labels(n) AS labels,
      count(n) AS count;

Inspect a direct Cypher pattern for grouping nodes by their labels while counting them. section 4.2

How Graph Algorithms for Data Science covers it

How deep it goes
Defines implicit grouping and demonstrates label, year, month, and day aggregations with concrete query results.
Examples
Uses node-label counts, tweet distributions by year and month, and top days ranked by created-tweet counts.
What it assumes
Assumes basic Cypher clauses and aggregation functions while teaching implicit grouping through node and tweet examples.
What you can do after
Apply it via Cypher to produce grouped counts from labels, properties, and extracted datetime attributes.
How it connects
Connects grouping keys with Cypher aggregation, count, labels, datetime attributes, WITH, RETURN, and WHERE.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.