Chapter 8. Generating Summaries
Section 8.0. Introduction Recipe 8.1. Summarizing with COUNT( ) Recipe 8.2. Summarizing with MIN( ) and MAX( ) Recipe 8.3. Summarizing with SUM( ) and AVG( ) Recipe 8.4. Using DISTINCT to Eliminate Duplicates Recipe 8.5. Finding Values Associated with Minimum and Maximum Values Recipe 8.6. Controlling String Case Sensitivity for MIN( ) and MAX( ) Recipe 8.7. Dividing a Summary into Subgroups Recipe 8.8. Summaries and NULL Values Recipe 8.9. Selecting Only Groups with Certain Characteristics Recipe 8.10. Using Counts to Determine Whether Values Are Unique Recipe 8.11. Grouping by Expression Results Recipe 8.12. Categorizing Noncategorical Data Recipe 8.13. Controlling Summary Display Order Recipe 8.14. Finding Smallest or Largest Summary Values Recipe 8.15. Date-Based Summaries
Recipe 8.16. Working with Per-
Recipe 8.17. Generating a Report That Includes a Summary and a List |
8.0. Introduction
Database systems are useful for storing and retrieving records, but they can also summarize your data in more
The examples just mentioned
Yet another kind of summary produces
The type of summaries that you generate may depend on the kind of data you're working with. A counting summary can be generated from any kind of values, whether they be
Summary operations in MySQL involve the following SQL constructs:
The recipes in this chapter first illustrate basic summary techniques, and then show how to perform more complex summary operations. You'll find additional examples of summary
Summary queries sometimes involve complex expressions. For summaries that you execute often, keep in mind that views can make queries easier to use. Section 3.12
The primary tables used for examples in this chapter are the
driver_log
and
mail
tables. These were also used heavily in Chapter 7, so they should look familiar. A third table used recurrently throughout the chapter is
states
, which has rows containing a few
mysql> SELECT * FROM states ORDER BY name; +----------------+--------+------------+----------+ name abbrev statehood pop +----------------+--------+------------+----------+ Alabama AL 1819-12-14 4530182 Alaska AK 1959-01-03 655435 Arizona AZ 1912-02-14 5743834 Arkansas AR 1836-06-15 2752629 California CA 1850-09-09 35893799 Colorado CO 1876-08-01 4601403 Connecticut CT 1788-01-09 3503604 ...
The
name
and
abbrev
columns list the full state name and the corresponding abbreviation. The
statehood
column indicates the day on which the state entered the Union.
pop
is the state population as of July, 2004, as
This chapter uses other tables occasionally as well. You can create most of them with the scripts found in the tables directory of the recipes distribution. Section 5.15 describes the kjv table. |