Полезная информация

next up previous contents index
Next: Using GROUP BY Up: SQL Aggregates Previous: SQL Aggregates

Aggregates

There are five aggregates outlined in table [*].

 
Table: Aggregates
Aggregate Function
COUNT(*) count of rows
SUM(colname) column total
MAX(colname) column maximum
MIN(colname) column minimum
AVG(colname) column average


COUNT operates on entire rows. The others operate on specific columns. Figure [*] shows examples of aggregate queries.
  
Figure: Aggregate examples
\begin{figure}\begin{list}{}{
\setlength{\rightmargin}{\leftmargin}
\raggedrigh...
...~friends;
\par avg
\par -{}-{}-
\par ~25
\par (1~row)\end{list}\par
\end{figure}

NULLs are skipped in column aggregates, but are processed by COUNT.

Aggregates can be combined with the WHERE clause to produce more complex results. The query SELECT AVG(age) FROM friends WHERE age >= 21 computes the average age of people age 21 or older. This prevents Dick Gleason from being included in the average computation because he is younger than 21. The column label defaults to the name of the aggregate. You can use AS to change it, as shown in section [*].


next up previous contents index
Next: Using GROUP BY Up: SQL Aggregates Previous: SQL Aggregates
Bruce Momjian
1999-11-21