Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j aggregation and sum

Tags:

neo4j

cypher

enter image description here

I apologies if my question is trivial i am a NOOB at neo4j.

I am trying to write a cypher query that will find all the purple nodes desc order from the value of [sum ( mass_of * contains ) for all paths going from purple to red ]

example: In picture it would be [( mass_of * contains )] for all red paths then sum all the red paths.

I started with this query but i m not sure where to go from here.

MATCH p0=(p:Purple)-[m:mass_of]->(g:Green)-[c:contains]->(r:red {name: "something"})
WITH m, c.amount * m.amount as total_per_path
WITH total_per_path, reduce( total=0, node IN collect(m)| total + total_per_path) AS total_something
RETURN total_something as TOTAL, total_per_path as PER_TOTAL_PATH

...

thanks for any help.

like image 739
user618589 Avatar asked Sep 29 '22 09:09

user618589


1 Answers

This should do it

MATCH (p:Purple)-[m:mass_of]->(g:Green)-[c:contains]->(r:red {name: "something"})
RETURN p, SUM(c.amount * m.amount) AS total
ORDER BY total DESC

You can also collect the m,g or c in return if you need that nodes/relationships.

like image 181
František Hartman Avatar answered Oct 18 '22 12:10

František Hartman