Skip to content

MIN

The Min aggregate function allows you to retrieve the smallest value for a set of objects. To use the function, include Min(columnNameInDatabase) into the list of properties requested from the server. For example, the following query retrieves the smallest box office earnings from the Moviesdata table:

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
[queryBuilder setPropertiesWithProperties:@[@"Min(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.setProperties(properties: ["Min(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 min property in the returned object. The value of the property is the smallest amount in the totalBoxOffice column for all movies in the database:

{  
    "___class" = Movie;  
    "__meta" = "{\"selectedProperties\":[\"min\"]}";  
    min = 1341511219;  
    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 "smallest" alphanumeric value for the a movie title:

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
[queryBuilder setPropertiesWithProperties:@[@"Min(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.setProperties(properties: ["Min(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 smallest value for the alphanumeric columns is determined by the alphabetical order of the characters. As a result, the movie starting with "A" is the one returned by the server:

{  
    "___class" = Movie;  
    "__meta" = "{\"selectedProperties\":[\"min\"]}";  
    min = Avatar;  
    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:

Min( columnNameInDatabase ) as customPropertyName

For example, the following query returns the minimum amount in the minimumTotal property:

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
[queryBuilder setPropertiesWithProperties:@[@"Min(totalBoxOffice) as minimumTotal"]];

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.setProperties(properties: ["Min(totalBoxOffice) as minimumTotal"])

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\"]}";  
    minimumTotal = 1341511219;  
    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 minimum earnings value for the movies in the database grouped by the year the movies were released:

DataQueryBuilder *queryBuilder = [DataQueryBuilder new];
[queryBuilder setPropertiesWithProperties:@[@"Min(totalBoxOffice)", @"yearReleased"]];
[queryBuilder setGroupByGroupBy:@[@"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.setProperties(properties: ["Min(totalBoxOffice)", "yearReleased"])
queryBuilder.setGroupBy(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\":[\"min\",\"yearReleased\"]}";  
        min = 2207615668;  
        objectId = "71F045B1-4A10-441A-FFF9-5005EF4FD400";  
        yearReleased = 1997;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"min\",\"yearReleased\"]}";  
        min = 2783918982;  
        objectId = "FEA0F8E2-44C0-3717-FFD9-28A7763E5500";  
        yearReleased = 2009;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"min\",\"yearReleased\"]}";  
        min = 1341511219;  
        objectId = "C1CF4388-96B0-6A37-FFAE-69A305E44100";  
        yearReleased = 2011;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"min\",\"yearReleased\"]}";  
        min = 1519479547;  
        objectId = "9B01F85E-BB31-84D9-FFF0-966F4CE78800";  
        yearReleased = 2012;  
    },  
        {  
        "___class" = Movie;  
        "__meta" = "{\"selectedProperties\":[\"min\",\"yearReleased\"]}";  
        min = 1408218722;  
        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 setPropertiesWithProperties:@[@"Min(totalBoxOffice)", @"yearReleased"]];
[queryBuilder setGroupByGroupBy:@[@"yearReleased"]];
[queryBuilder setSortBySortBy:@[@"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.setProperties(properties: ["Min(totalBoxOffice)", "yearReleased"])
queryBuilder.setGroupBy(groupBy: ["yearReleased"])
queryBuilder.setSortBy(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 setPropertiesWithProperties:@[@"Min(totalBoxOffice) as minTotal", @"yearReleased"]];
[queryBuilder setGroupByGroupBy:@[@"yearReleased"]];
[queryBuilder setSortBySortBy:@[@"minTotal"]];

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.setProperties(properties: ["Min(totalBoxOffice) as minTotal", "yearReleased"])
queryBuilder.setGroupBy(groupBy: ["yearReleased"])
queryBuilder.setSortBy(sortBy: ["minTotal"])

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