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 dataQueryBuilder = DataQueryBuilder.create().setProperties( "Avg(totalBoxOffice)");
Backendless.Data.of( "Movie" ).find( dataQueryBuilder, new AsyncCallback<List<Map>>()
{
  @Override
  public void handleResponse( List<Map> response )
  {
    // print out the first object from the collection
    Log.i( "MYAPP", response.get( 0 );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", fault.toString() ); 
  }
});
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:
basicAverageResponse

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 dataQueryBuilder = DataQueryBuilder.create();
dataQueryBuilder.setProperties( "Avg(totalBoxOffice) as averageTotalBoxOffice" );
Backendless.Data.of( "Movie" ).find( dataQueryBuilder, new AsyncCallback<List<Map>>()
{
  @Override
  public void handleResponse( List<Map> response )
  {
    // print out the first object from the collection
    Log.i( "MYAPP", response.get( 0 );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", fault.toString() ); 
  }
});
The response for the query now contains the averageTotalBoxOffice property:

basicAverageWithAliasResponse

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 dataQueryBuilder = DataQueryBuilder.create();
dataQueryBuilder.setProperties( "Avg(totalBoxOffice)", "yearReleased" );
dataQueryBuilder.setGroupBy( "yearReleased" );
Backendless.Data.of( "Movie" ).find( dataQueryBuilder, new AsyncCallback<List<Map>>()
{
  @Override
  public void handleResponse( List<Map> response )
  {
    Log.i( "MYAPP", response );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", fault.toString() ); 
  }
});
The response for this request contains average earning values for the movies grouped by the release year:

basicAverageWithGroupBy

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 dataQueryBuilder = DataQueryBuilder.create();
dataQueryBuilder.setProperties( "Avg(totalBoxOffice)", "yearReleased" );
dataQueryBuilder.setGroupBy( "yearReleased" );
dataQueryBuilder.setSortBy( "yearReleased DESC" );
Backendless.Data.of( "Movie" ).find( dataQueryBuilder, new AsyncCallback<List<Map>>()
{
  @Override
  public void handleResponse( List<Map> response )
  {
    Log.i( "MYAPP", response );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", fault.toString() ); 
  }
});
To sort results by the aggregated value, assign a custom name to the column and sort by that name:
DataQueryBuilder dataQueryBuilder = DataQueryBuilder.create();
dataQueryBuilder.setProperties( "Avg(totalBoxOffice) as averageTotal", "yearReleased" );
dataQueryBuilder.setGroupBy( "yearReleased" );
dataQueryBuilder.setSortBy( "averageTotal" );
Backendless.Data.of( "Movie" ).find( dataQueryBuilder, new AsyncCallback<List<Map>>()
{
  @Override
  public void handleResponse( List<Map> response )
  {
    Log.i( "MYAPP", response );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
    Log.e( "MYAPP", fault.toString() ); 
  }
});