cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Need to create a trigger to Sum the node property

Hi Guys

I am having property nodes of student like
{studentId:"1", English:20}
{studentId:"1", Maths:20}
{studentId:"1", physics:20}

I need to have final node like
{studentId:"1",total:60}

Is it possible using Trigger?

Can anyone help on this situation??

1 REPLY 1

create nodes

MERGE (s:Student {studentId: "1",name:"1"})
MERGE (math:Subject:Math {marks: 20})
MERGE (eng:Subject:English {marks: 20})
MERGE (physics:Subject:Physics {marks: 20})
MERGE (s)-[:HAS_SUBJECT]->(math)
MERGE (s)-[:HAS_SUBJECT]->(eng)
MERGE (s)-[:HAS_SUBJECT]->(physics)

and run aggregate query

MATCH (s:Student)-[r:HAS_SUBJECT]->(sub:Subject)
WITH s, sum(sub.marks) as total
MERGE (s)-[:TOTAL_MARKS]->(t:Total {marks:total})
RETURN *;

You can use trigger as well:

call apoc.trigger.add("total","MATCH (s:Student)-[r:HAS_SUBJECT]->(sub:Subject)
WITH s, sum(sub.marks) as total
MERGE (s)-[:TOTAL_MARKS]->(t:Total {marks:total})",{phase:'before'});