cancel
Showing results forΒ
Did you mean:Β

Head's Up! Site maintenance this Wednesday, February 1. Disruptions expected as we migrate the forums.

## How to divide the weight of a connection by the largest weight among all connections?

I have relations with different values in property and want divide all values by the maximum value and return result as new property for relationship/ How can i do that?

Do I have to first collect all the weight values of all relations, or is it possible somehow easier?

1 ACCEPTED SOLUTION

The code almost worked, but I had to remove the "n" after "with"

Working code

``````match(n:Root)-[r:REL]->(m:Child)
with max(r.value) as maxValue, collect(r) as rels
unwind rels as rel
set rel.scaledValue = rel.value / maxValue``````

4 REPLIES 4
Ninja

You can try something like this:

``````match(n:Root)-[r:REL]->(m:Child)
with n, max(r.value) as maxValue, collect(r) as rels
unwind rels as rel
set rel.scaledValue = rel.value / maxValue``````

The code almost worked, but I had to remove the "n" after "with"

Working code

``````match(n:Root)-[r:REL]->(m:Child)
with max(r.value) as maxValue, collect(r) as rels
unwind rels as rel
set rel.scaledValue = rel.value / maxValue``````

I guess you have multiple Root nodes and you want the max across all the relationships of type REL, regardless of the Root node they are connected too.

Anyways, I am glad you got it to work.

Ninja

As an alternative form, you can also use 'forEach' instead of 'unwind':

``````match(n:Root)-[r:REL]->(m:Child)
with n, max(r.value) as maxValue, collect(r) as rels
forEach(rel in rels |
set rel.scaledValue = rel.value / maxValue
)``````