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.

Operations between related nodes of the same relation type

Greetings,
I have a question about how to perform operations between children nodes. I have a document node(s) with about ~1200 children each containing a single value of a named metric. I'd like to pull two specific related node to create a ratio, then repeat for a list of parent nodes.
I'm thinking of using a user def function or a change in the modeling, but hoping for another opinion.

for example:

match( documents:Document )-[:hasMetric]->(x:Metric {name="revenue })
match( documents:Document )-[:hasMetric]->(y:Metric {name="profit })
...
// for every document, return y.value / x.value

the model is very simple right now as I do not want to over complicate it yet, but is that my issue? Maybe each named metric needs its own relation type, but that would be 1,000s of relation types on millions of nodes.

thanks for any reply, and please let me know how to clarify.
Cheers.

1 ACCEPTED SOLUTION

Hi sullirobert,

welcome to the community!

I am not sure if I got the whole problem and feel free to clarify if there is more but for your example you could do something like:

MATCH (x:Metric {name:"revenue"})<-[:hasMetric]-( documents:Document )-[:hasMetric]->(y:Metric {name:"profit" })
RETURN y.value/x.value

If you have more than two metrics, probably something like the following will work:

MATCH ( documents: Document )
WITH documents
MATCH (documents)-[:hasMetric]->(x:Metric {name:"revenue" }),  (documents)-[:hasMetric]->(y:Metric {name:"profit" }), (documents)-[:hasMetric]->(z:Metric {name:"cost" })
RETURN ...

Regards,
Elena

View solution in original post

2 REPLIES 2

Hi sullirobert,

welcome to the community!

I am not sure if I got the whole problem and feel free to clarify if there is more but for your example you could do something like:

MATCH (x:Metric {name:"revenue"})<-[:hasMetric]-( documents:Document )-[:hasMetric]->(y:Metric {name:"profit" })
RETURN y.value/x.value

If you have more than two metrics, probably something like the following will work:

MATCH ( documents: Document )
WITH documents
MATCH (documents)-[:hasMetric]->(x:Metric {name:"revenue" }),  (documents)-[:hasMetric]->(y:Metric {name:"profit" }), (documents)-[:hasMetric]->(z:Metric {name:"cost" })
RETURN ...

Regards,
Elena

yup. works! thank you