All Collections
Data Analysis
Gremlin training
Gremlin Training 19: Basic aggregation steps - sum()
Gremlin Training 19: Basic aggregation steps - sum()

Learn to use the sum()-step to sum up the numeric values of a collection of components or references.

Kristine Marhilevica avatar
Written by Kristine Marhilevica
Updated over a week ago

While the count()-step works on a collection of components or references, the sum step works on a collection of numerical values.

The following query returns the sum of all of the "total_direct_cost" fields on applications.

g.V().
hasLabel('Application').
values('total_direct_cost').
sum()

sum()-step

Bonus: To add two field values on the same component, we may use the following query. Subtraction (-), multiplication (*), and division (/) can be done by changing the sign in the map.

g.V(ids).
has('field_a').
has('field_b').
project('id', 'name', 'value').
by(id).
by('name').
by(map{ it.get().value('field_a') +
it.get().value('field_b') })

Did this answer your question?