Skip to content

Model Class

minimum()

minimum() — returns any

Available in: model Category: Statistics Functions

Calculates the minimum value for a specified property in a model using SQL’s MIN() function. This can be used to find the lowest value of a numeric property across all records or with conditions. You can also include associations, handle soft-deleted records, provide fallback values, and group results. 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 lowest 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.
parameterizeanynotrueSet to true to use cfqueryparam on all columns, or pass in a list of property names to use cfqueryparam on those only.
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. Basic Minimum Value
// Get the lowest salary among all employees
lowestSalary = model("employee").minimum("salary");
writeOutput("Lowest Salary: " & lowestSalary);
// inline: writeOutput(model("employee").minimum("salary"));

2. Minimum Value with Condition
// Get the lowest salary for employees in a specific department
deptId = 5;
lowestSalary = model("employee").minimum(
    property="salary",
    where="departmentId=#deptId#"
);
writeOutput("Lowest Salary in Department #deptId#: " & lowestSalary);
// inline: writeOutput(model("employee").minimum(property="salary", where="departmentId=5"));

3. Minimum Value with Range and Fallback
// Get the lowest salary within a range and fallback to 0 if no records
lowestSalary = model("employee").minimum(
    property="salary",
    where="salary BETWEEN #params.min# AND #params.max#",
    ifNull=0
);
writeOutput("Lowest Salary in range: " & lowestSalary);
// inline: writeOutput(model("employee").minimum(property="salary", where="salary BETWEEN #params.min# AND #params.max#", ifNull=0));

4. Including Associations
// Get the lowest product price including related categories
lowestPrice = model("product").minimum(
    property="price",
    include="category"
);
writeOutput("Lowest Product Price: " & lowestPrice);
// inline: writeOutput(model("product").minimum(property="price", include="category"));

5. Include Soft-Deleted Records
// Include soft-deleted employees in the calculation
lowestSalary = model("employee").minimum(
    property="salary",
    includeSoftDeletes=true
);
writeOutput("Lowest Salary including soft-deleted employees: " & lowestSalary);
// inline: writeOutput(model("employee").minimum(property="salary", includeSoftDeletes=true));