maximum()
Description
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.
Function Syntax
maximum(property [, where, include, parameterize, ifNull, includeSoftDeletes ])
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
property |
string |
Yes | |
Name of the property to get the highest value for (must be a property of a numeric data type). |
where |
string |
No | |
This argument maps to the WHERE clause of the query. 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. You do not need to specify the table name(s); Wheels will do that for you. |
include |
string |
No | |
Associations 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. |
parameterize |
any |
No | true |
Set to true to use cfqueryparam on all columns, or pass in a list of property names to use cfqueryparam on those only. |
ifNull |
any |
No | |
The 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. |
includeSoftDeletes |
boolean |
No | false |
You can set this argument to true to include soft-deleted records in the results. |
Examples
<!--- Get the amount of the highest salary for all employees --->
<cfset highestSalary = model("employee").maximum("salary")>
<!--- Get the amount of the highest salary for employees in a given department --->
<cfset highestSalary = model("employee").maximum(property="salary", where="departmentId=#params.key#")>
<!--- Make sure a numeric value is always returned, even if no records are found to calculate the maximum for --->
<cfset highestSalary = model("employee").maximum(property="salary", where="salary > #params.minSalary#", ifNull=0)>
