apoc.agg.medianFunction
| Syntax | 
 | ||
| Description | Returns the mathematical median for all non-null  | ||
| Arguments | Name | Type | Description | 
| 
 | 
 | A value to be aggregated. | |
| Returns | 
 | ||
Usage examples
The examples in this section are based on the following sample graph:
CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (TomH:Person {name:'Tom Hanks', born:1956})
CREATE (TheDevilsAdvocate:Movie {title:"The Devil's Advocate", released:1997, tagline:'Evil has its winning ways'})
CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})
CREATE (SleeplessInSeattle:Movie {title:'Sleepless in Seattle', released:1993, tagline:'What if someone you never met, someone you never saw, someone you never knew was the only someone for you?'})
CREATE (ThatThingYouDo:Movie {title:'That Thing You Do', released:1996, tagline:'In every life there comes a time when that thing you dream becomes that thing you do'})
CREATE (YouveGotMail:Movie {title:"You've Got Mail", released:1998, tagline:'At odds in life... in love on-line.'})
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixReloaded)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheDevilsAdvocate)
CREATE (TomH)-[:ACTED_IN {roles:['Joe Fox']}]->(YouveGotMail)
CREATE (TomH)-[:ACTED_IN {roles:['Sam Baldwin']}]->(SleeplessInSeattle)
CREATE (TomH)-[:ACTED_IN {roles:['Mr. White']}]->(ThatThingYouDo);We can find the median release year of the movies acted in by each person, by running the query below:
apoc.agg.median
MATCH (p:Person)-[:ACTED_IN]->(movie)
RETURN p.name AS person, apoc.agg.median(movie.released) AS medianReleaseYearCypher’s COLLECT subqueries and CASE expressions
MATCH (p:Person)
WITH p, COLLECT { MATCH (p)-[:ACTED_IN]->(movie) RETURN movie.released ORDER BY movie.released } AS releasedYears
WITH p.name AS name, releasedYears, size(releasedYears) AS size
RETURN name,
    CASE size % 2
        WHEN 1 THEN releasedYears[size / 2]
        ELSE (releasedYears[size / 2 - 1] + releasedYears[size / 2]) / 2
    END AS medianReleaseYear| person | medianReleaseYear | 
|---|---|
| "Keanu Reeves" | 1999.0 | 
| "Tom Hanks" | 1996.0 |