Aggregate functions operate on a set of data and return a single value. Use these functions for retrieving summary information from a table, as opposed to retrieving an entire table and then operating on the record set of the entire table.
Consider using aggregate functions to perform the following operations:
Since not every relational database management system (RDBMS) supports all aggregate functions, refer to your database's documentation. The following table lists the aggregate functions that ColdFusion supports:
Function | Description |
---|---|
AVG() |
Returns the average (mean) for a column. |
COUNT() |
Returns the number of rows in a column. |
MAX() |
Returns the largest value of a column. |
MIN() |
Returns the lowest value of a column. |
SUM() |
Returns the sum of values of a column. |
aggregate_func ::= <COUNT>(* | column_name) | AVG | SUM | MIN | MAX) ([ALL | DISTINCT] numeric_exp)
The following example uses the AVG()
function to retrieve the average IQ of all terriers:
SELECT dog_name, AVG(dog_IQ) AS avg_IQ FROM Dogs WHERE breed LIKE '%Terrier';
ColdFusion supports aggregate functions of any arbitrary expression, as follows:
SELECT lorange, count(lorange+hirange) FROM roysched GROUP BY lorange;
ColdFusion supports mathematical expressions that include aggregate functions, as follows:
SELECT MIN(lorange) + MAX(hirange) FROM roysched GROUP BY lorange;