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

Aggregate value expression

The aggregate value expression holds an aggregate function instance and returns the execution result of the aggregate function.

Aggregate value expressions can be used together with GROUP BY clauses in RETURN statements to group the result set by one or more columns. For more information, see RETURN statement.

Syntax

aggregate_value_expression
aggregate_function ( DISTINCT ALL value_expression , )

aggregate_value_expression ::=
    aggregate_function '(' ( 'DISTINCT' | 'ALL' )? value_expression ( ',' value_expression )* ')' 

Parameters

aggregate_function

The name of an aggregate function. For more information about aggregate functions, see Aggregate functions.

DISTINCT | ALL

DISTINCT and ALL are specified to indicate whether duplicate values are included in the specified group.

  • If you specify DISTINCT, the aggregate function ignores duplicate values in the specified group.
  • If you specify ALL, the aggregate function includes duplicate values in the specified group.
  • If you do not specify DISTINCT or ALL, the default is ALL.

value_expression

A value that is taken as an argument by the aggregate function. The value can be of any data type. For more information, see Value expressions.

Examples

The following examples are based on the example dataset.

Aggregation operations on a single group of values

FOR i in LIST [1,2,3,3,4,4] RETURN count(DISTINCT i), avg(i), sum(i), collect(DISTINCT i) GROUP BY ()

The preceding statement uses GROUP BY () to group the entire result set as a single group and uses the count, avg, sum, and collect aggregate functions to perform aggregation operations on the group of rows. The count(DISTINCT i) expression returns the count of distinct values in the group. The avg(i) expression, equivalent to avg(ALL i), returns the average of all values in the group. The sum(i) expression, equivalent to sum(ALL i), returns the sum of all values in the group. The collect(DISTINCT i) expression returns a list consisting of the distinct values in the group.

Return:

+-------------------+--------------------+--------+---------------------+
| count(DISTINCT i) | avg(i)             | sum(i) | collect(DISTINCT i) |
+-------------------+--------------------+--------+---------------------+
| 4                 | 2.8333333333333335 | 17     | [4,3,2,1]           |
+-------------------+--------------------+--------+---------------------+

Aggregation operations on the result of a MATCH statement

'''
USE movie 
MATCH ()-[e:Watch]->(v@Movie {id: 472}) 
RETURN collect(e.rate), avg(e.rate) GROUP BY ()
'''

The preceding statement returns all the ratings of the movie with ID 472 and calculates the average rating of the movie.

Return:

+-----------------+-------------+
| collect(e.rate) | avg(e.rate) |
+-----------------+-------------+
| [3.0]           | 3.0         |
+-----------------+-------------+