5.15
Aggregate Functions in SQL
Aggregate functions are those functions that operate on sets of values. Typical
examples include: sum, avg, max, min, and count.
The first four functions operate on “columns” of tables and ignore null values.
The count returns the number of elements of the set that is its argument.
Example 5.15.1 The following select construct determines the largest grade
obtained by the student whose student number is 1011. The function max is
applied to the set of grades of the student whose number is 1011 and returns
the largest value in this set:
select max(grade) as highgr from GRADES
where stno = ’1011’;
This returns the table:
HIGHGR
------
90
For instance, sum(A) returns the sum of all values of the selected nonnull
A-components of the tuples. Similarly, avg(A) returns the average value of the
same sequence. The expressions max(A) and min(A) yield the largest and the
smallest values in the set of A-components of the tuples selected by a query,
respectively.
The functions sum and avg apply to attributes whose domains are numerical
(such as integer or float); max and min apply to every kind of attribute.
If we wish to discard duplicate values from the sequences of values before
applying these functions, we need to use the word distinct. For instance,
sum(distinct A) considers only the distinct nonnull values that occur in the
sequence of components.
Example 5.15.2 We mentioned that the built-in functions max and min apply
to string domains as well as to numerical domains. We use this feature of these
functions to determine the first and the last student in alphabetical order:
5.15Aggregate Functions in SQLAggregate functions are those functions that operate on sets of values. Typicalexamples include: sum, avg, max, min, and count. The first four functions operate on “columns” of tables and ignore null values.The count returns the number of elements of the set that is its argument.Example 5.15.1 The following select construct determines the largest gradeobtained by the student whose student number is 1011. The function max isapplied to the set of grades of the student whose number is 1011 and returnsthe largest value in this set:select max(grade) as highgr from GRADES where stno = ’1011’;This returns the table:HIGHGR------90 For instance, sum(A) returns the sum of all values of the selected nonnullA-components of the tuples. Similarly, avg(A) returns the average value of thesame sequence. The expressions max(A) and min(A) yield the largest and thesmallest values in the set of A-components of the tuples selected by a query,respectively. The functions sum and avg apply to attributes whose domains are numerical(such as integer or float); max and min apply to every kind of attribute. If we wish to discard duplicate values from the sequences of values beforeapplying these functions, we need to use the word distinct. For instance,sum(distinct A) considers only the distinct nonnull values that occur in thesequence of components.Example 5.15.2 We mentioned that the built-in functions max and min applyto string domains as well as to numerical domains. We use this feature of thesefunctions to determine the first and the last student in alphabetical order:
การแปล กรุณารอสักครู่..
