Skip to content

AVERAGE

The Avg aggregate function allows you to calculate the average value for a set of objects. To use the function, include Avg(columnNameInDatabase) into the list of properties requested from the server. For example, the following query retrieves an average box office earnings value for all movies in the database:

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.AddProperty( "Avg(totalBoxOffice)" );

// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result;
result = Backendless.Data.Of( "Movie" ).Find( queryBuilder );

// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
  aggrResult =>
  {
    // print out the first object from the collection
    System.Console.WriteLine( aggrResult[ 0 ] );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of( "Movie" ).Find( queryBuilder, findCallback );
The query returns the following result, notice the avg property in the returned object. The value of the property is the mathematical average of all values in the totalBoxOffice column:
Using Alias

To change the name of the returned property from avg to a custom name, use the following syntax:

Avg( columnNameInDatabase ) as customPropertyName

For example, the following query returns the average value in the averageTotalBoxOffice property:

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.AddProperty( "Avg(totalBoxOffice) as averageTotalBoxOffice" );

// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result;
result = Backendless.Data.Of( "Movie" ).Find( queryBuilder );

// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
  aggrResult =>
  {
    // print out the first object from the collection
    System.Console.WriteLine( aggrResult[ 0 ] );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of( "Movie" ).Find( queryBuilder, findCallback );
The response for the query now contains the averageTotalBoxOffice property:

Grouping Results

Results in the response can be grouped by values from another column. To request grouping of the results, add the groupBy parameter to the request with the value containing the name of the column. For example, the following request returns the average box office earnings value for the movies in the database grouped by the year the movies were released:

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.AddProperty( "Avg(totalBoxOffice)" );
queryBuilder.AddProperty( "yearReleased" );
queryBuilder.AddGroupBy( "yearReleased" );

// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result;
result = Backendless.Data.Of( "Movie" ).Find( queryBuilder );

// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
  aggrResult =>
  {
    // print out the first object from the collection
    System.Console.WriteLine( aggrResult[ 0 ] );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of( "Movie" ).Find( queryBuilder, findCallback );
The response for this request contains average earning values for the movies grouped by the release year:

Sorting

The results can be sorted using the sortBy parameter. For example, results for the following request will be sorted by the values in the yearReleased column in the descending order:

DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.AddProperty( "Avg(totalBoxOffice)" );
queryBuilder.AddProperty( "yearReleased" );
queryBuilder.AddGroupBy( "yearReleased DESC" );

// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result;
result = Backendless.Data.Of( "Movie" ).Find( queryBuilder );

// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
  aggrResult =>
  {
    // print out the first object from the collection
    System.Console.WriteLine( aggrResult[ 0 ] );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of( "Movie" ).Find( queryBuilder, findCallback );
To sort results by the aggregated value, assign a custom name to the column and sort by that name:
DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.AddProperty( "Avg(totalBoxOffice) as averageTotal" );
queryBuilder.AddProperty( "yearReleased" );
queryBuilder.AddGroupBy( "averageTotal" );

// ***********************************************************
// Blocking API:
// ***********************************************************
IList<Dictionary<string, object>> result;
result = Backendless.Data.Of( "Movie" ).Find( queryBuilder );

// ***********************************************************
// Non-blocking API:
// ***********************************************************
AsyncCallback<IList<Dictionary<string, object>>> findCallback;
findCallback = new AsyncCallback<IList<Dictionary<string, object>>>(
  aggrResult =>
  {
    // print out the first object from the collection
    System.Console.WriteLine( aggrResult[ 0 ] );
  },
  error =>
  {
    System.Console.WriteLine( "Server returned an error " + error.Message );
  } );

Backendless.Data.Of( "Movie" ).Find( queryBuilder, findCallback );

Codeless Reference

Consider the following data table called Movie:

data_service_avg_min_max_example

The example below calculates an average box office value earnings for all movies in the database. In the context of this example, the properties argument expects the aggregate function Avg(columnName) to make a query.

data_service_example_average

Important

For a detailed description of all input parameters see the Basic Object Retrieval topic of this guide.

After the Codeless logic runs the operation returns one object containing the average value:

data_service_example_average_2

Using Alias

Any default property returned in the response can have a custom name. You can change the default property name (e.g. avg) returned in the response by defining a dynamic property as shown in the excerpt below. The totalBoxOffice is the name of the column in the data table, and the averageTotalBoxOffice is the custom property name that will be shown in the response instead of the current property name (e.g. avg):

Avg(totalBoxOffice) as averageTotalBoxOffice

The example below calculates an average for all values stored in the "totalBoxOffice" column and returns the result with a custom property name "averageTotalBoxOffice":

data_service_example_average_3

After the Codeless logic runs, the operation returns an object with a new property name averageTotalBoxOffice:

data_service_example_average_4

Grouping The Results

Results can be grouped by values from another column. To group results you have to reference the name of the column in the group by property. By doing so, you instruct the operation to separate every calculated average value into a distinct object based on the value from the column you have used in the group by property.

As you reference the column name in the group by property, the operation groups all values first, and then it calculates an average:

The example below groups values in the totalBoxOffice column/property by values in the yearReleased column/property into distinct objects and calculates an average for every object. For better perception of the operation result you can also include the yearReleased in the properties argument, so that you can see an average value for every year in the response.

data_service_example_average_5

The operation has returned a list of objects containing an average value of total earnings(averageTotalBoxOffice property) grouped by the values in the yearReleased column. As you can see, every object is separated by values from the yearReleased column.

data_service_example_average_6

Sorting

You can sort the data by referencing the column/property name in the sort by property.

Results for the following request will be sorted by the values in the yearReleased column in the descending order:

data_service_example_average_7

As you can see, objects returned in the response are sorted in descending order by values in the yearReleased column/property:

data_service_example_average_8