Some queries examine aspects of the data in your table that reflect properties of groups of rows rather than of individual rows. For example, you may want to find the average amount of money that a customer pays for an order, or to see how many employees work for each department. For these types of tasks, you use aggregate functions and the GROUP BY clause.
In Interactive SQL, execute the following query:
SELECT COUNT( * ) FROM Employees; |
COUNT() |
---|
75 |
The result set consists of only one column, with title COUNT(*), and one row, which contains the total number of employees.
In Interactive SQL, execute the following query:
SELECT COUNT( * ), MIN( BirthDate ), MAX( BirthDate ) FROM Employees; |
COUNT() | MIN(Employees.BirthDate) | MAX(Employees.BirthDate) |
---|---|---|
75 | 1936-01-02 | 1973-01-18 |
The functions COUNT, MIN, and MAX are called aggregate functions. Aggregate functions summarize information. Other aggregate functions include statistical functions such as AVG, STDDEV, and VARIANCE. All but COUNT require a parameter.
Aggregate functions return a single value for a set of rows. If there is no GROUP BY clause, the aggregate function is called a scalar aggregate and it returns a single value for all the rows that satisfy other aspects of the query. If there is a GROUP BY clause, the aggregate is termed a vector aggregate and it returns a value for each group.
SQL Anywhere supports additional aggregate functions for analytics, sometimes referred to as OLAP functions. Several of these functions can be used as window functions: they include RANK, PERCENT_RANK, CUME_DIST, ROW_NUMBER, and functions to support linear regression analysis.
Applying aggregate functions to grouped data
Restricting groups
Combination of WHERE and HAVING clauses
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |