Wednesday, August 8, 2007

AGGREGATE FUNCTIONS

Aggregate functions return a single value based upon a
set of other values.

If used among other expressions in the item list of a SELECT
statement, the SELECT must have a GROUP BY or HAVING clause.
No GROUP BY or HAVING clause is required if the aggregate
function is the only value retrieved by the SELECT statement.

EX:


SUM(expression) - Computes the sum of column values given by
expression.

COUNT(expression) - Counts the rows defined by the expression.

COUNT(*) Counts all rows in the specified table or
view.


MIN(expression) - Finds the minimum value in a column given
by expression.

MAX(expression) - Finds the maximum value in a column given
by expression.

RANK(value_list) WITHIN GROUP (ORDER BY sort_list)

Generates a rank for a hypothetical row (value_list) in a group
of rows generated by GROUP BY.


The number of values processed by an aggregate function varies
depending on the number of rows queried from the table.
This behavior makes aggregate functions different from scalar
functions, which can only operate on the values of a single row
per invocation.

The general syntax of an aggregate function is:

aggregate_function_name ( [ALL | DISTINCT] expression )


The aggregate function name may be AVG, COUNT, MAX, MIN, or SUM.

All aggregate functions except COUNT(*) will ignore NULL values
when computing their results.

No comments: