Previous | Table of Contents | Next |
Today is all about SQL aggregates. Aggregates are functions that take information stored in a single column and report a single result. Todays lesson covers the following points:
Lets start out by defining what an aggregate really is.
An aggregate is a SQL Server function that returns a single value for a set of rows in a column.
Five aggregates are defined by T-SQL. Table 5.1 lists the five functions and what they do.
Name | Description |
---|---|
SUM | Total of all non-null values |
AVG | Average of all non-null values |
MIN | The lowest non-null value (minimum) |
MAX | The highest non-null value (maximum) |
COUNT | Counts the number of non-null values |
Notice that aggregates do not operate on null values. When null values are included in the aggregation, that row is effectively left out of the calculation. See the next main section, titled Aggregates with Null, for a discussion on this topic.
You can take two paths now; if you think you get the aggregate functions, you should try a few on your own or skip to the exercises at the end of the chapter. Then, pick up todays topics again at Using Count, because that aggregate has a few exceptions. If youd like to see some examples of the aggregates, though, Ill demonstrate each of them operating on the price column in the titles table.
Lets start with a simple example. To total up the prices of all the booksmaybe somebody wanted to know what it would cost for one of everythinguse this query:
select sum(price) from titles
---------------- 236.26 (1 row(s) affected)
All the books that have a pricethat is, all books where the price is not nullare added up and returned in response to this query.
Notice that the row count, appearing after the result set, shows that only one row was affected. Although the sum operated on 16 rows, which is the number of non-null rows in the titles table, where aggregates are concerned, the row count reports the number of rows that were returned.
One last thing Id like to mention is the name of the sum column in the result set. It doesnt have a name.
It wouldnt really apply to call this column price, because what you got back is the total of that column. When any kind of calculation is performed on a column, including aggregate functions, the column name will be blank. If you ask for price * 1, youll get back a blank column heading. If you want to name the column, youll have to provide a column alias. Column aliasing was covered on the second day.
The AVG aggregate returns the average of the column values. If you want the average price of all the books in the titles table, you would use this query:
select avg(price) Average from titles
Average ---------------------- 14.77
The average price for a book in the titles table is $14.77. Null priced books are not included in the calculation; they do not push the average price up or down.
You may request multiple aggregates in the same SELECT. In this example, you request the lowest-priced book and the highest-priced book in the titles table:
select min(price) Cheapest, max(price) Most Expensive from titles
Cheapest Most Expensive --------------------------------- 2.99 22.95 (1 row(s) affected)
The minimum value in the price column is $2.99; the maximum, $22.95.
Technical Note: If you ask for multiple aggregates in a single SELECT, the server wont use an index to resolve the query. It may be faster to run two separate queries or to use a subquery, like this:select min(price) Cheapest, (select max(price) Most Expensive from titles) from titlesSubqueries are covered fully on Day 8.
The COUNT aggregate is used to get the exact number of rows that match a set of conditions. So far, the examples havent used a WHERE clause with an aggregate. (I talk about that in the section Aggregates with WHERE.) COUNT can take three forms. It can be used with a column name, with an asterisk, or with the DISTINCT keyword.
COUNT with a Column Name
Here is the example Ive been using with the other aggregates. For a count of the number of non-null values in the price column, use this query:
select count(price) from titles
-------------------- 16
There are 16 rows in the titles table that have a value in their price column.
Previous | Table of Contents | Next |