5
votes

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.

1
So it seems like your query is mostly there, but I don't understand why you're doing that collect on the basis of m - that bit is confusing because m is a relationship, but then you're saying node in collect(m) which doesn't really make sense, and then in the expression after it you're not using node. So it seems like total_per_path mostly has what you want....why not sum all of the total_per_path variables?FrobberOfBits
I did the sum on total_per_path but that sums all the oranges and all the red paths. I want to have the sum for all the red paths and the sum for all the orange paths. I was trying to collect by m because it's represents a path ( but that does not seam quite right )user618589

1 Answers

4
votes

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.