Aggregation in Cypher

  1. count() – count the number of occurrences. There are two ways to count return results: count(n) and count(*). The first method counts the number of occurrences of n and the second method counts the number of result rows returned

  2. collect() – allows you to aggregate values into a list

  3. size() – find the number of items in the list

Chaining Queries

You can use the WITH clause to pass values from one section of a query to another. This allows you to execute and save intermediate calculations to be use later. See example below:

MATCH (a:Person)-[r:LIKES]-(t:Technology)

WITH a.name AS name, collect(t.type) AS technologies

RETURN name, technologies

The WITH clause requires all the values passed to have a variable. WITH clause is also useful for setting up parameters before the query.

Looping through List

You can use UNWIND to inspect the elements in the list. UNWIND is the opposite of collect().

WITH [4, 5, 6, 7] AS experienceRange

UNWIND experienceRange AS number

MATCH (p:Person)

WHERE p.yearsExp = number

RETURN p.name, p.yearsExp

Ordering Results

ORDER BY allows us to sort the results ascendingly or descendingly based on the value you specified.

WITH [4, 5, 6, 7] AS experienceRange

UNWIND experienceRange AS number

MATCH (p:Person)

WHERE p.yearsExp = number

RETURN p.name, p.yearsExp ORDER BY p.yearsExp DESC

Return Unique Results

This is possible with DISTINCT clause.

Return N number of results

This is possible with LIMIT N clause.

Ryan

Ryan

Data Scientist

Leave a Reply