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:

GET https://xxxx.backendless.app/api/data/Movie?props=Max(totalBoxOffice)

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:

[  
    {  
        "max": 2783918982,  
        "___class": "Movie",  
    }  
]

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:

GET https://xxxx.backendless.app/api/data/Movie?props=Max(title) 

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:

[  
    {  
        "min": "Titanic",  
        "___class": "Movie",  
    }  
]

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:

Important

The  value must be URL-encoded. The spaces are replaced with %20:

GET https://xxxx.backendless.app/api/data/Movie?props=Max(totalBoxOffice)%20as%20maxTotal

The response for the query now contains the minimumTotal property:

[  
    {  
        "maxTotal": 2783918982,  
        "___class": "Movie",  
    }  
]

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:

GET https://xxxx.backendless.app/api/data/Movie?props=Max(totalBoxOffice),yearReleased&groupBy=yearReleased

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

[  
    {  
        "max": 2207615668,  
        "yearReleased": 1997,  
        "___class": "Movie",  
    },  
    {  
        "max": 2783918982,  
        "yearReleased": 2009,  
        "___class": "Movie",  
    },  
    {  
        "max": 1341511219,  
        "yearReleased": 2011,  
        "___class": "Movie",  
    },  
    {  
        "max": 1519479547,  
        "yearReleased": 2012,  
        "___class": "Movie",  
    },  
    {  
        "max": 2058662225,  
        "yearReleased": 2015,  
        "___class": "Movie",  
    }  
]

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:

GET https://xxxx.backendless.app/api/data/Movie?props=Max(totalBoxOffice),yearReleased&groupBy=yearReleased&sortBy=yearReleased%20DESC

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

GET https://xxxx.backendless.app/api/data/Movie?props=Max(totalBoxOffice)%20as%20maxTotal,yearReleased&groupBy=yearReleased&sortBy=minTotal