MAX¶
The Max
aggregate function allows you to retrieve the largest value for a set of objects. To use the function, include Max(columnNameInDatabase)
into the list of properties requested from the server. For example, the following query retrieves the largest box office earnings from the Movies
data table:
DataQueryBuilder dataQueryBuilder = DataQueryBuilder.create().setProperties( "Max(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() );
}
});
max
property in the returned object. The value of the property is the largest amount in the totalBoxOffice
column for all movies in the database:
The function can be applied not only to the columns containing numerical values. For example, the following query retrieves the "largest" alphanumeric value for the a movie title:
DataQueryBuilder dataQueryBuilder = DataQueryBuilder.create().setProperties( "Max(title)");
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() );
}
});
Using Alias¶
To change the name of the returned property from min
to a custom name, use the following syntax:
Max( columnNameInDatabase ) as customPropertyName
For example, the following query returns the maximum amount in the maximumTotal
property:
DataQueryBuilder dataQueryBuilder = DataQueryBuilder.create();
dataQueryBuilder.setProperties( "Max(totalBoxOffice) as maxTotal");
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() );
}
});
minimumTotal
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 maximum earnings value for the movies in the database grouped by the year the movies were released:
DataQueryBuilder dataQueryBuilder = DataQueryBuilder.create();
dataQueryBuilder.setProperties( "Max(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() );
}
});
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( "Max(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() );
}
});
DataQueryBuilder dataQueryBuilder = DataQueryBuilder.create();
dataQueryBuilder.setProperties( "Max(totalBoxOffice) as maxTotal", "yearReleased" );
dataQueryBuilder.setGroupBy( "yearReleased" );
dataQueryBuilder.setSortBy( "maxTotal" );
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() );
}
});