Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.
Texas State Technical College DISCOVER! Aggregate Functions At times, it may be necessary to obtain information about the entire set of records obtained from a SELECT statement. Such information might include a field maximum, minimum, average, or count. Aggregate functions calculate information across a group or set of records. Overview
Texas State Technical College DISCOVER! Aggregate functions work in an aggregate mode. Any fields or calculations given as a parameter to an aggregate function are accessed/calculated for every record in the group. Overview Aggregate Functions
Texas State Technical College DISCOVER! The most common aggregate functions are as follows: count() Counts either all records or the number of records that have a value in a field.count() Counts either all records or the number of records that have a value in a field. avg() Calculates the average value across a field in a group.avg() Calculates the average value across a field in a group. sum() Calculates the total of all the values in a field for the group.sum() Calculates the total of all the values in a field for the group. max() Calculates the maximum value in the group.max() Calculates the maximum value in the group. min() Calculates the minimum value in the group.min() Calculates the minimum value in the group. Common Aggregate Functions Aggregate Functions
Texas State Technical College DISCOVER! Determines either the number of records in the group or the number of records with a value in a field Syntax Number count(*) Counts all records Number count(FIELD fieldname) Counts records where value in fieldname is NOT NULL ExampleSELECTcount(*)FROMborrower; Count Aggregate Functions
Texas State Technical College DISCOVER! Determines the average value for a field or calculation across all records. Syntax Number avg(FIELD fieldName | CALCULATION cValue) ExampleSELECT avg(debt)AS “Average Customer Debt”, avg(debt – paid) AS “Average Customer Balance” FROMborrower; Avg Aggregate Functions
Texas State Technical College DISCOVER! Determines the total value across a set of records. Syntax Number sum(FIELD fieldName | CALCULATION cValue) ExampleSELECT sum(debt)AS “Total Debt Due Company”, sum(debt – paid)AS “Total Balance Owed to Company” sum(debt – paid)AS “Total Balance Owed to Company”FROMborrower; Sum Aggregate Functions
Texas State Technical College DISCOVER! Determines the maximum value in a field or calculation across a set of records. Syntax Number max(FIELD fieldName | CALCULATION cValue) ExampleSELECT max(debt)AS “Greatest Borrower”, max(debt – paid)AS “Greatest Delinquent Borrower” max(debt – paid)AS “Greatest Delinquent Borrower”FROMborrower; Max Aggregate Functions
Texas State Technical College DISCOVER! Determines the minimum value in a field or calculation across a set of records. Syntax Number min(FIELD fieldName | CALCULATION cValue) ExampleSELECT min(debt)AS “Least Borrower”, min(debt – paid)AS “Least Delinquent Borrower” min(debt – paid)AS “Least Delinquent Borrower”FROMborrower; Min Aggregate Functions
Texas State Technical College DISCOVER! The function itself is aggregate; it works across a group of records. However, the function must operate at a single- row level to retrieve the field or calculation value from each record. The parameters given to an aggregate function are evaluated in a single-row mode. Important Aggregate Functions
Texas State Technical College DISCOVER! In the function sum(debt) the field value of debt is evaluated for each and every record in the group and the result is added to the group sum. In the function sum(debt – paid) sum(debt – paid) the calculation (debt – paid) is evaluated for each and every record in the group and the result is added to the group sum. For Example… Aggregate Functions
Texas State Technical College DISCOVER! When used alone, aggregate functions see all the records in a SELECT statement as one complete group. To further divide the records into subgroups, use the GROUP BY statement. At this point, the SELECT statement will apply to each GROUP and the aggregate function will calculate for each group. Caveats Aggregate Functions
Texas State Technical College DISCOVER! In Summary… Aggregate functions calculate information about a set or group of records. The parameters to aggregate functions are evaluated at the single-row level. By default, they group all the records in the return set into one group. If a GROUP BY statement is used, the aggregate functions perform calculations for each group. Aggregate Functions