Skip to content

Instantly share code, notes, and snippets.

@adam-cowley
Created April 20, 2023 12:29
Show Gist options
  • Save adam-cowley/789abe70b4352f4622fc7e67413ce2a5 to your computer and use it in GitHub Desktop.
Save adam-cowley/789abe70b4352f4622fc7e67413ce2a5 to your computer and use it in GitHub Desktop.
Next-level Cypher aggregations using WITH, COLLECT and UNWIND!
MATCH (u:User)-[:HAS_ENROLMENT]->(e:CompletedEnrolment)
WHERE e.completedAt >= datetime('2021-01-01')
WITH
e.completedAt.year +'-'+ e.completedAt.quarter AS quarter,
count(*) AS count
WITH collect({quarter: quarter, count: count}) AS all
UNWIND range(0, size(all)-1) AS idx
RETURN idx,
all[idx].quarter AS quarter,
all[idx].count AS count,
CASE WHEN idx > 0 THEN apoc.math.round((100.0 * all[idx].count / all[idx+1].count), 2) ELSE null END AS change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment