Skip to content
Developer Edition runs on one host and does not require a product license file. Need multi-host deployment or performance testing at scale? Explore Enterprise Edition

AvgAgg

An AvgAgg aggregator value variable (AvgAgg<T>) maintains the average of all inputs, where T represents the data type of the input values.

Declaration syntax

avg_agg_declaration
NODE VALUE aggregator_value_variable AvgAgg < INT8 INT16 INT32 INT64 FLOAT DOUBLE >

avg_agg_declaration ::=
'NODE'? 'VALUE' aggregator_value_variable 'AvgAgg' '<' ('INT8'|'INT16'|'INT32'|'INT64'|'FLOAT'|'DOUBLE') '>'

For information about the parameters in the declaration syntax, see Parameters.

Initial value

As indicated in the declaration syntax, an initial value must not be specified during declaration.

Input type and supported operations

The input type must be T, which is specified during declaration.

Both assignment and aggregation operations are supported:

  • Assignment operation (=): This operation resets the value of the aggregate value variable. For example, SET @avg_agg = 10 clears the value held by the aggregate value variable avg_agg and sets it to 10. At this point, the total is 10, the count is 1, and the average is 10.
  • Aggregation operation (+=): This operation accumulates the value into the aggregate value variable and recalculates the average value based on all accumulated values. For example, if avg_agg already has a value, executing SET @avg_agg += 10 does not clear the existing value. Instead, it first adds the value 10 to the accumulated sum of avg_agg and increases the count. It then automatically updates its average based on all accumulated values.

Return type

AvgAgg<T> -> DOUBLE

The return type is always DOUBLE regardless of the input type.

Examples

The following examples are based on the temporary graph #movie_temporary_graph created in CREATE TEMPORARY GRAPH.

Global AvgAgg aggregator value variables

'''
USE #movie_temporary_graph {
  VALUE global_avg AvgAgg<INT>
  SET @global_avg += 3     // Average: 3.0 (Total: 3, Count: 1)
  SET @global_avg += 7     // Average: 5.0 (Total: 10, Count: 2)
  SET @global_avg += 5     // Average: 5.0 (Total: 15, Count: 3)
  LOG_INFO("@global_avg: ",@global_avg)
  SET @global_avg = 0      // avg: 0 (reset to 0)
  LOG_INFO("@global_avg: ",@global_avg)
}
'''

Return:

Log:
2025-08-21T08:04:55.522902 INFO  - @global_avg: 5
2025-08-21T08:04:55.524437 INFO  - @global_avg: 0

Node-attached AvgAgg aggregator value variables

'''
USE #movie_temporary_graph {
  NODE VALUE node_avg AvgAgg<DOUBLE>

  MATCH (a@User WHERE a.id IN [1,3,4])-[w@Watch]->(b@Movie)
  PER PATH {
    // Accumulate user ratings for movies watched and calculate the average
    // Track each user's average rating for movies watched
    SET a.@node_avg += w.rate
  }
  PER NODE (a) {
    LOG_INFO("a.id: ",a.id, " a.@node_avg: ",a.@node_avg)
  }
}
'''

Return:

Log:
2025-08-21T08:33:36.002357 INFO  - a.id: 4 a.@node_avg: 2.66667
2025-08-21T08:33:36.002357 INFO  - a.id: 3 a.@node_avg: 3.75
2025-08-21T08:33:36.002357 INFO  - a.id: 1 a.@node_avg: 4.77778