apoc.agg.last
Syntax |
|
||
Description |
Returns the last value from the given collection. |
||
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 (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 (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})
CREATE (TheMatrixResurrections:Movie {title:'The Matrix Resurrections', released:2021, tagline:'The choice is yours. Return to the source. Re-enter.'})
CREATE (YouveGotMail:Movie {title:"You've Got Mail", released:1998, tagline:'At odds in life... in love on-line.'})
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 (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixReloaded)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixRevolutions)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixResurrections)
CREATE (TomH)-[:ACTED_IN {roles:['Joe Fox']}]->(YouveGotMail)
CREATE (TomH)-[:ACTED_IN {roles:['Sam Baldwin']}]->(SleeplessInSeattle);
The following examples return the latest movie that the above actors starred in using both APOC and Cypher:
apoc.agg.last
MATCH (p:Person)-[:ACTED_IN]->(movie)
WITH p, movie
ORDER BY p, movie.released
RETURN p.name AS person, apoc.agg.last(movie) AS latestMovie;
Using Cypher’s CALL subquery
MATCH (p:Person)
CALL (p) {
MATCH (p)-[:ACTED_IN]->(movie)
RETURN movie ORDER BY movie.released DESC LIMIT 1
}
RETURN p.name AS person, movie AS earliestMovie ORDER BY p
person | latestMovie |
---|---|
"Keanu Reeves" |
(:Movie { "title": "The Matrix Resurrections", "tagline": "The choice is yours. Return to the source. Re-enter.", "released": 2021 }) |
"Tom Hanks" |
(:Movie { "title": "You’ve Got Mail", "tagline": "At odds in life… in love on-line.", "released": 1998}) |