Skip to content

Model Class

maximum()

maximum() — returns any

Available in: model Category: Statistics Functions

Calculates the maximum value for a given property. Uses the SQL function MAX. If no records can be found to perform the calculation on you can use the ifNull argument to decide what should be returned.

NameTypeRequiredDefaultDescription
propertystringyesName of the property to get the highest value for (must be a property of a numeric data type).
wherestringnoMaps to the WHERE clause of the query (or HAVING when necessary). The following operators are supported: =, !=, <>, <, <=, >, >=, LIKE, NOT LIKE, IN, NOT IN, IS NULL, IS NOT NULL, AND, and OR (note that the key words need to be written in upper case). You can also use parentheses to group statements. Nested queries not allowed. You do not need to specify the table name(s); Wheels will do that for you.
includestringnoAssociations that should be included in the query using INNER or LEFT OUTER joins (which join type that is used depends on how the association has been set up in your model). If all included associations are set on the current model, you can specify them in a list (e.g. department,addresses,emails). You can build more complex include strings by using parentheses when the association is set on an included model, like album(artist(genre)), for example. These complex include strings only work when returnAs is set to query though.
parameterizeanynotrue
ifNullanynoThe value returned if no records are found. Common usage is to set this to 0 to make sure a numeric value is always returned instead of a blank string.
includeSoftDeletesbooleannofalseSet to true to include soft-deleted records in the queries that this method runs.
groupstringnoMaps to the GROUP BY clause of the query. You do not need to specify the table name(s); Wheels will do that for you.
1. Maximum value for all records
highestSalary = model("employee").maximum("salary");
// one-liner: highestSalary = model("employee").maximum("salary");

2. Maximum value with a WHERE condition
highestSalary = model("employee").maximum(
    property="salary", 
    where="departmentId=#params.departmentId#"
);
// one-liner: highestSalary = model("employee").maximum(property="salary", where="departmentId=#params.departmentId#");

3. Maximum value with a default if no records found
highestSalary = model("employee").maximum(
    property="salary", 
    where="salary > #params.minSalary#", 
    ifNull=0
);
// one-liner: highestSalary = model("employee").maximum(property="salary", where="salary > #params.minSalary#", ifNull=0);

4. Maximum value including associations (nested join)
highestAlbumSales = model("album").maximum(
    property="sales",
    include="artist(genre)"
);
// one-liner: highestAlbumSales = model("album").maximum(property="sales", include="artist(genre)");

5. Maximum value grouped by a column
maxSalaryByDept = model("employee").maximum(
    property="salary",
    group="departmentId"
);
// one-liner: maxSalaryByDept = model("employee").maximum(property="salary", group="departmentId");