< Day Day Up > |
Column functions compute, from a group of rows, a single value for a designated column or expression. This provides the capability to aggregate data, thereby enabling you to perform statistical calculations across many rows with one SQL statement. To fully appreciate the column functions, you must understand SQL's set-level processing capabilities. This list shows some rules for the column functions:
A column function can be specified in a WHERE clause only if that clause is part of a subquery of a HAVING clause. Additionally, every column name specified in the expression of the column function must be a correlated reference to the same group. The column functions are AVG , COUNT , COUNT_BIG , MAX , MIN , STDDEV , SUM , and VARIANCE . The AVG FunctionThe AVG function computes the average of the values for the column or expression specified as an argument. This function operates only on numeric arguments. The following example calculates the average salary of each department: SELECT WORKDEPT, AVG(SALARY) FROM DSN8810.EMP GROUP BY WORKDEPT; The AVG function is the preferred method of calculating the average of a group of values. Although an average, in theory, is nothing more than a sum divided by a count, DB2 may not return equivalent values for AVG(COL_NAME) and SUM(COL_NAME)/COUNT(*) . The reason is that the COUNT function will count all rows regardless of value, whereas SUM ignores nulls. The COUNT FunctionThe COUNT function counts the number of rows in a table, or the number of distinct values for a given column. It can operate , therefore, at the column or row level. The syntax differs for each. To count the number of rows in the EMP table, issue this SQL statement: SELECT COUNT(*) FROM DSN8810.EMP; It does not matter what values are stored in the rows being counted. DB2 will simply count the number of rows and return the result. To count the number of distinct departments represented in the EMP table, issue the following SELECT COUNT(DISTINCT WORKDEPT) FROM DSN8810.EMP; The keyword DISTINCT is not considered an argument of the function. It simply specifies an operation to be performed before the function is applied. When DISTINCT is coded, duplicate values are eliminated. If DISTINCT is not specified, then ALL is implicitly specified. ALL also can be explicitly specified in the COUNT function. When ALL is specified, duplicate values are not eliminated. NOTE The argument of the COUNT function can be of any built-in data type other than a large object: CLOB , DBCLOB , or BLOB . Character string arguments can be no longer 255 bytes and graphic string arguments can be no longer than 127 bytes. The result of the COUNT function cannot be null. COUNT always returns an INTEGER value greater than or equal to zero. The COUNT_BIG FunctionThe COUNT_BIG function is similar to the COUNT function. It counts the number of rows in a table, or the number of distinct values for a given column. However, the COUNT_BIG function returns a result of data type DECIMAL(31,0) , whereas COUNT can return a result only as large as the largest DB2 integer value, namely +2,147,483,647 . The COUNT_BIG function works the same as the COUNT function, except it returns a decimal value. Therefore, the example SQL for COUNT is applicable to COUNT_BIG . Simply substitute COUNT_BIG for COUNT . For example, the following statement counts the number of rows in the EMP table (returning a decimal value, instead of an integer): SELECT COUNT_BIG(*) FROM DSN8810.EMP; NOTE The COUNT_BIG function has the same restrictions as the COUNT function. The argument of the COUNT_BIG function can be of any built-in data type other than a large object: CLOB , DBCLOB , or BLOB . Character string arguments can be no longer than 255 bytes and graphic string arguments can be no longer than 127 bytes. The result of the COUNT_BIG function cannot be null. COUNT_BIG returns a decimal value greater than or equal to zero. The MAX FunctionThe MAX function returns the largest value in the specified column or expression. The following SQL statement determines the project with the latest end date: SELECT MAX(ACENDATE) FROM DSN8810.PROJACT; NOTE The result of the MAX function is of the same data type as the column or expression on which it operates. The argument of the MAX function can be of any built-in data type other than a large object: CLOB , DBCLOB , or BLOB . Character string arguments can be no longer than 255 bytes and graphic string arguments can be no longer than 127 bytes. A somewhat more complicated example using MAX is shown below. It returns the largest salary paid to a man in department D01 : SELECT MAX(SALARY) FROM DSN8810.EMP WHERE WORKDEPT = 'D01' AND SEX = 'M'; The MIN FunctionThe MIN function returns the smallest value in the specified column or expression. To retrieve the smallest bonus given to any employee, issue this SQL statement: SELECT MIN(BONUS) FROM DSN8810.EMP; NOTE The result of the MIN function is of the same data type as the column or expression on which it operates. The argument of the MIN function can be of any built-in data type other than a large object: CLOB , DBCLOB , or BLOB . Character string arguments can be no longer than 255 bytes and graphic string arguments can be no longer than 127 bytes. The STDDEV FunctionThe STDDEV function returns the standard deviation of a set of numbers . The standard deviation is calculated at the square root of the variance. For example SELECT STDDEV(SALARY) FROM DSN8810.EMP WHERE WORKDEPT = 'D01'; NOTE The argument of the STDDEV function can be any built-in numeric data type. The resulting standard deviation is a double precision floating-point number. The SUM FunctionThe accumulated total of all values in the specified column or expression are returned by the SUM column function. For example, the following SQL statement calculates the total yearly monetary output for the corporation: SELECT SUM(SALARY+COMM+BONUS) FROM DSN8810.EMP; This SQL statement adds each employee's salary, commission, and bonus. It then aggregates these results into a single value representing the total amount of compensation paid to all employees . NOTE The argument of the SUM function can be any built-in numeric data type. The resulting sum must be within the range of acceptable values for the data type. For example, the sum of an INTEGER column must be within the range “2,147,483,648 to +2,147,483,647 . This is because the data type of the result is the same as the data type of the argument values, except:
The VARIANCE FunctionThe VARIANCE function returns the variance of a set of numbers. The result is the biased variance of the set of numbers. The variance is calculated as follows : VARIANCE = SUM(X**2)/COUNT(X) - (SUM(X)/COUNT(X))**2 NOTE The argument of the VARIANCE function can be any built-in numeric data type. The resulting variance is a double precision floating-point number. For brevity and ease of coding, VARIANCE can be shortened to VAR . |
< Day Day Up > |