Skip to content

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 Moviesdata table:

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
queryBuilder.properties = @[@"Max(totalBoxOffice)"];

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Movie"];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *foundObjects) {
    // print out the first object from the collection
    NSLog(@"%@", foundObjects.firstObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.properties = ["Max(totalBoxOffice)"]

let dataStore = Backendless.shared.data.ofTable("Movie")
dataStore.find(queryBuilder: queryBuilder, responseHandler: { foundObjects in
    // print out the first object from the collection
    if let first = foundObjects.first {
        print("\(first)")
    }
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

The query returns the following result, notice the 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:

{  
    "___class" = Movie;  
    "__meta" = "{\"selectedProperties\":[\"min\"]}";  
    max = 2783918982;  
    objectId = "71F045B1-4A10-441A-FFF9-5005EF4FD400";  
}

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 *queryBuilder = [DataQueryBuilder new];
queryBuilder.properties = @[@"Max(title)"];

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Movie"];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *foundObjects) {
    // print out the first object from the collection
    NSLog(@"%@", foundObjects.firstObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.properties = ["Max(title)"]

let dataStore = Backendless.shared.data.ofTable("Movie")
dataStore.find(queryBuilder: queryBuilder, responseHandler: { foundObjects in
    // print out the first object from the collection
    if let first = foundObjects.first {
        print("\(first)")
    }
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

The largest value for the alphanumeric columns is determined by the alphabetical order of the characters. As a result, a movie positioned to be the last one when ordered alphabetically  is the one returned by the server:

{  
    "___class" = Movie;  
    "__meta" = "{\"selectedProperties\":[\"min\"]}";  
    max = Titanic;  
    objectId = "71F045B1-4A10-441A-FFF9-5005EF4FD400";  
}

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 *queryBuilder = [DataQueryBuilder new];
queryBuilder.properties = @[@"Max(totalBoxOffice) as maxTotal"];

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Movie"];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *foundObjects) {
    // print out the first object from the collection
    NSLog(@"%@", foundObjects.firstObject);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.properties = ["Max(totalBoxOffice) as maxTotal"]

let dataStore = Backendless.shared.data.ofTable("Movie")
dataStore.find(queryBuilder: queryBuilder, responseHandler: { foundObjects in
    // print out the first object from the collection
    if let first = foundObjects.first {
        print("\(first)")
    }
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

The response for the query now contains the minimumTotal property:

{  
    "___class" = Movie;  
    "__meta" = "{\"selectedProperties\":[\"min\"]}";  
    maxTotal = 2783918982;  
    objectId = "71F045B1-4A10-441A-FFF9-5005EF4FD400";  
}

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 *queryBuilder = [DataQueryBuilder new];
queryBuilder.properties = @[@"Max(totalBoxOffice)", @"yearReleased"];
queryBuilder.groupBy = @[@"yearReleased"];

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Movie"];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *foundObjects) {
    NSLog(@"%@", foundObjects);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.properties = ["Max(totalBoxOffice)", "yearReleased"]
queryBuilder.groupBy = ["yearReleased"]

let dataStore = Backendless.shared.data.ofTable("Movie")
dataStore.find(queryBuilder: queryBuilder, responseHandler: { foundObjects in
    print("\(foundObjects)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

The response for this request contains average earning values for the movies grouped by the release year:

(  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"max\",\"yearReleased\"]}";  
        max = 2207615668;  
        objectId = "71F045B1-4A10-441A-FFF9-5005EF4FD400";  
        yearReleased = 1997;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"max\",\"yearReleased\"]}";  
        max = 2783918982;  
        objectId = "FEA0F8E2-44C0-3717-FFD9-28A7763E5500";  
        yearReleased = 2009;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"max\",\"yearReleased\"]}";  
        max = 1341511219;  
        objectId = "C1CF4388-96B0-6A37-FFAE-69A305E44100";  
        yearReleased = 2011;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"max\",\"yearReleased\"]}";  
        max = 1519479547;  
        objectId = "9B01F85E-BB31-84D9-FFF0-966F4CE78800";  
        yearReleased = 2012;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"max\",\"yearReleased\"]}";  
        max = 2516748684;  
        objectId = "A42CC872-BED0-8738-FF93-7326E1A38100";  
        yearReleased = 2015;  
    }  
)

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 new];
queryBuilder.properties = @[@"Max(totalBoxOffice)", @"yearReleased"];
queryBuilder.groupBy = @[@"yearReleased"];
queryBuilder.sortBy = @[@"yearReleased DESC"];

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Movie"];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *foundObjects) {
    NSLog(@"%@", foundObjects);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.properties = ["Max(totalBoxOffice)", "yearReleased"]
queryBuilder.groupBy = ["yearReleased"]
queryBuilder.sortBy = ["yearReleased DESC"]

let dataStore = Backendless.shared.data.ofTable("Movie")
dataStore.find(queryBuilder: queryBuilder, responseHandler: { foundObjects in
    print("\(foundObjects)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

To sort results by the aggregated value, assign a custom name to the column and sort by that name:

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
queryBuilder.properties = @[@"Max(totalBoxOffice) as maxTotal", @"yearReleased"];
queryBuilder.groupBy = @[@"yearReleased"];
queryBuilder.sortBy = @[@"maxTotal"];

MapDrivenDataStore *dataStore = [Backendless.shared.data ofTable:@"Movie"];
[dataStore findWithQueryBuilder:queryBuilder responseHandler:^(NSArray *foundObjects) {
    NSLog(@"%@", foundObjects);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
let queryBuilder = DataQueryBuilder()
queryBuilder.properties = ["Max(totalBoxOffice) as maxTotal", "yearReleased"]
queryBuilder.groupBy = ["yearReleased"]
queryBuilder.sortBy = ["maxTotal"]

let dataStore = Backendless.shared.data.ofTable("Movie")
dataStore.find(queryBuilder: queryBuilder, responseHandler: { foundObjects in
    print("\(foundObjects)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Codeless Reference

Consider the following data table called Movie:

data_service_count_example

The example below extracts the greatest value from the totalBoxOffice column. To retrieve the greatest value you must use the aggregate function Max(columnName) in the properties argument:

data_service_max_example_1

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 an object with the max property containing the value which reflects the greatest of all values stored in the requested column/property:

data_service_max_example_2

Using Alias

Any default property returned in the response can have a custom name. You can change the default property name (e.g. max) 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 greatestBoxOffice is the custom property name that will be shown in the response instead of the current property name (e.g. max):

Max(totalBoxOffice) as greatestBoxOffice

The example below retrieves the greatest value stored in the "totalBoxOffice" column and returns the result with a custom property name "greatestBoxOffice":

data_service_max_example_3

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

data_service_max_example_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 create an object for every group of values retrieved from the column referenced in the group by property.

As you reference the column name in the group by property, the operation groups all values first into separate objects, and then it finds the greatest of all values.

The example below groups values in the totalBoxOffice column by values in the yearReleased column into distinct objects and then finds the greatest value. For better perception of the operation result you can also include the yearReleased column in the properties argument, so that you could see the greatest box office earnings for every year in the response.

data_service_max_example_5

First, the operation has grouped all records into distinct objects based on the values in the yearReleased column passed to the group by property. Then for every object the operation has returned the greatest of all values stored in the totalBoxOffice column.

data_service_max_example_6

Sorting

You can sort the data by referencing the column 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_max_example_7

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

data_service_max_example_8

The function can be applied not only to the columns containing numerical values. For example, the following query retrieves the "largest" alphanumeric value for a movie title:

data_service_max_example_9

After the Codeless Logic runs, the operation returns an object containing the movieName custom property and the corresponding "largest" alphanumerical value which is "Takers".

data_service_max_example_10