Home > Net >  neo4j How to get a subset using a forloop as a subquery?
neo4j How to get a subset using a forloop as a subquery?

Time:08-30

For each person, I want to get the first 5 events and last 5 events (based on eventTime). I want to compare the winrate of the first 5 event to the last to see the most improved person. I am struggling to find a way to handle the for loop logic in neo4j.

GDB Schema:

(p: Person) -[:PlaysIn]-> (e:Event {eventTime:, eventOutcome:})

CodePudding user response:

The apoc.coll.sortNodes function does the trick for you. See https://neo4j.com/labs/apoc/4.1/overview/apoc.coll/apoc.coll.sortNodes/

MATCH (p:Person)
WITH p, apoc.coll.sortNodes([(p)-[:PlaysIn]->(e:Event) | e ], 'eventTime') AS events
RETURN p,
       events[0..5] AS first5Events,
       events[..-5] AS last5Events
  • Related