Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

13
Texas State Technical College DISCOVER! Aggregate Functions Aggregate Functions It’s a group thing.

Transcript of Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Page 1: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

Aggregate FunctionsAggregate Functions

It’s a group thing.

Page 2: 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 At times, it may be necessary to obtain information about the entire set of records information about the entire set of records obtained from a SELECT statement.obtained from a SELECT statement.

Such information might include a field Such information might include a field maximum, minimum, average, or count.maximum, minimum, average, or count.

Aggregate functions calculate information Aggregate functions calculate information across a group or set of records.across a group or set of records.

OverviewOverview

Page 3: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

Aggregate functions work in an aggregate Aggregate functions work in an aggregate mode.mode.

Any fields or calculations given as a Any fields or calculations given as a parameter to an aggregate function are parameter to an aggregate function are accessed/calculated for every record in accessed/calculated for every record in the group.the group.

OverviewOverview

Aggregate Functions

Page 4: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

The most common aggregate functions are The most common aggregate functions are as follows:as follows:

• count()count()Counts either all records or the number of records that have a Counts either all records or the number of records that have a value in a field.value in a field.

• avg()avg()Calculates the average value across a field in a group.Calculates the average value across a field in a group.

• sum()sum()Calculates the total of all the values in a field for the group.Calculates the total of all the values in a field for the group.

• max()max()Calculates the maximum value in the group.Calculates the maximum value in the group.

• min()min()Calculates the minimum value in the group.Calculates the minimum value in the group.

Common Aggregate FunctionsCommon Aggregate Functions

Aggregate Functions

Page 5: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

Determines either the number of records in Determines either the number of records in the group or the number of records with a the group or the number of records with a value in a fieldvalue in a field

SyntaxSyntaxNumber count(*)Number count(*)

Counts all recordsCounts all recordsNumber count(FIELD fieldname) Number count(FIELD fieldname)

Counts records where value in fieldname is NOT NULLCounts records where value in fieldname is NOT NULL

ExampleExampleSELECTSELECT

count(*)count(*)FROMFROM

borrowerborrower;;

CountCount

Aggregate Functions

Page 6: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

Determines the average value for a field or Determines the average value for a field or calculation across all records.calculation across all records.

SyntaxSyntaxNumber avg(FIELD fieldName | CALCULATION cValue)Number avg(FIELD fieldName | CALCULATION cValue)

ExampleExampleSELECTSELECT

avg(debt)avg(debt) AS “Average Customer Debt”,AS “Average Customer Debt”,avg(debt – paid) AS “Average Customer Balance”avg(debt – paid) AS “Average Customer Balance”

FROMFROMborrowerborrower

;;

AvgAvg

Aggregate Functions

Page 7: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

Determines the total value across a set of Determines the total value across a set of records.records.

SyntaxSyntaxNumber sum(FIELD fieldName | CALCULATION cValue)Number sum(FIELD fieldName | CALCULATION cValue)

ExampleExampleSELECTSELECT

sum(debt)sum(debt) AS “Total Debt Due Company”,AS “Total Debt Due Company”, sum(debt – paid)sum(debt – paid) AS “Total Balance Owed to AS “Total Balance Owed to

Company”Company”FROMFROM

borrowerborrower;;

SumSum

Aggregate Functions

Page 8: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

Determines the maximum value in a field or Determines the maximum value in a field or calculation across a set of records.calculation across a set of records.

SyntaxSyntaxNumber max(FIELD fieldName | CALCULATION cValue)Number max(FIELD fieldName | CALCULATION cValue)

ExampleExampleSELECTSELECT

max(debt)max(debt) AS “Greatest Borrower”,AS “Greatest Borrower”, max(debt – paid)max(debt – paid) AS “Greatest Delinquent AS “Greatest Delinquent

Borrower”Borrower”FROMFROM

borrowerborrower;;

MaxMax

Aggregate Functions

Page 9: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

Determines the minimum value in a field or Determines the minimum value in a field or calculation across a set of records.calculation across a set of records.

SyntaxSyntaxNumber min(FIELD fieldName | CALCULATION cValue)Number min(FIELD fieldName | CALCULATION cValue)

ExampleExampleSELECTSELECT

min(debt)min(debt) AS “Least Borrower”,AS “Least Borrower”, min(debt – paid)min(debt – paid) AS “Least Delinquent AS “Least Delinquent

Borrower”Borrower”FROMFROM

borrowerborrower;;

MinMin

Aggregate Functions

Page 10: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

The function itself is aggregate; it works The function itself is aggregate; it works across a group of records.across a group of records.

However, the function must operate at a However, the function must operate at a single-row level to retrieve the field or single-row level to retrieve the field or calculation value from each record.calculation value from each record.

The parameters given to an aggregate The parameters given to an aggregate function are evaluated in a single-row function are evaluated in a single-row mode.mode.

ImportantImportant

Aggregate Functions

Page 11: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

In the functionIn the function

sum(debt)sum(debt)

the field value of debt is evaluated for each the field value of debt is evaluated for each and and every record in the group and the result is every record in the group and the result is added added to the group sum.to the group sum.

In the functionIn the function

sum(debt – paid)sum(debt – paid)

the calculation (debt – paid) is evaluated for the calculation (debt – paid) is evaluated for each each and every record in the group and the result and every record in the group and the result is is added to the group sum.added to the group sum.

For Example…For Example…

Aggregate Functions

Page 12: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

When used alone, aggregate functions see all When used alone, aggregate functions see all the records in a SELECT statement as one the records in a SELECT statement as one complete group.complete group.

To further divide the records into subgroups, To further divide the records into subgroups, use the GROUP BY statement.use the GROUP BY statement.

At this point, the SELECT statement will apply At this point, the SELECT statement will apply to each GROUP and the aggregate function to each GROUP and the aggregate function will calculate for each group.will calculate for each group.

CaveatsCaveats

Aggregate Functions

Page 13: Texas State Technical College DISCOVER! Aggregate Functions It’s a group thing.

Texas State Technical College

DISCOVER!

In Summary…In Summary…

Aggregate functions calculate information about a Aggregate functions calculate information about a set or group of records.set or group of records.

The parameters to aggregate functions are The parameters to aggregate functions are evaluated at the single-row level.evaluated at the single-row level.

By default, they group all the records in the return By default, they group all the records in the return set into one group.set into one group.

If a GROUP BY statement is used, the aggregate If a GROUP BY statement is used, the aggregate functions perform calculations for each group.functions perform calculations for each group.

Aggregate Functions