Model Class
sum()
Signature
Section titled “Signature”sum() — returns any
Available in: model
Category: Statistics Functions
Description
Section titled “Description”Calculates the total of all values for a given property (column) using SQL’s SUM() function. It’s typically used to aggregate numeric values across a set of records (e.g., summing salaries, prices, or quantities). You can add filtering with where, group results with group, or join associations using include. If no records are found, use the ifNull argument to return a safe default (commonly 0 for numeric sums).
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
property | string | yes | — | Name of the property to get the sum for (must be a property of a numeric data type). |
where | string | no | — | Maps 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. |
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. |
distinct | boolean | no | false | When true, SUM returns the sum of unique values only. |
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 | Set to true to include soft-deleted records in the queries that this method runs. |
group | string | no | — | Maps to the GROUP BY clause of the query. You do not need to specify the table name(s); Wheels will do that for you. |
Examples
Section titled “Examples”1. Basic sum
allSalaries = model("employee").sum("salary");
2. With filtering (where)
allAustralianSalaries = model("employee").sum(
property="salary",
include="country",
where="countryname='Australia'"
);
3. With ifNull safeguard
salarySum = model("employee").sum(
property="salary",
where="salary BETWEEN #params.min# AND #params.max#",
ifNull=0
);
4. Sum with grouping
salariesByDept = model("employee").sum(
property="salary",
group="departmentId"
);
5. Distinct sum
uniqueSalaries = model("employee").sum(
property="salary",
distinct=true
);