Simplifying/ Generalizing a cypher query


(IM) #1

Dear all,

I am relatively new to Neo4j and need it for university. I am working with the enterprise edition for education, with the latest version and the apoc plugin. I have a problem which I guess is resolved simply but I just cannot find a way how. Please see the code below. It works perfectly fine. However, please imagine there are up to hundreds of different caseIds (here only caseId 1 and 2 for illustration). I cannot write a separate query for every single case Id. Is there a way to simplify it? I have tried a lot of different things but if I write a more general statement by myself I always end up losing the order by caseId and things are just ordered by time.

USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM "file:///running_to_csv.csv" AS row
WITH toInteger(row.case_id) AS cid, row
CREATE (event: Event {caseId: cid, activityName: row.activity, time: row.timestamp})

MATCH(event: Event)
WHERE event.caseId = 1
WITH event ORDER BY event.time ASC 
WITH apoc.coll.frequencies(apoc.coll.pairsMin(COLLECT(event.activityName))) AS g
UNWIND g AS p
RETURN*

MATCH(event: Event)
WHERE event.caseId = 2
WITH event ORDER BY event.time ASC 
WITH apoc.coll.frequencies(apoc.coll.pairsMin(COLLECT(event.activityName))) AS g
UNWIND g AS p
RETURN*

Thank you!


(Ameyasoft) #2

Hi,

Try this:

MATCH(event: Event)
WITH event.activityName ORDER BY event.caseId ASC, event.time ASC
WITH apoc.coll.frequencies(apoc.coll.pairsMin(COLLECT(event.activityName))) AS g
UNWIND g AS p
RETURN*

-Kamal