Recipe 8.12. Categorizing Noncategorical Data


Problem

You need to summarize a set of values that are not naturally categorical.

Solution

Use an expression to group the values into categories.

Discussion

Section 8.11 showed how to group rows by expression results. One important application for doing so is to provide categories for values that are not particularly categorical. This is useful because GROUP BY works best for columns with repetitive values. For example, you might attempt to perform a population analysis by grouping rows in the states table using values in the pop column. As it happens, that does not work very well due to the high number of distinct values in the column. In fact, they're all distinct, as the following query shows:

mysql> SELECT COUNT(pop), COUNT(DISTINCT pop) FROM states; +------------+---------------------+ | COUNT(pop) | COUNT(DISTINCT pop) | +------------+---------------------+ |         50 |                  50 | +------------+---------------------+ 

In situations like this, where values do not group nicely into a small number of sets, you can use a transformation that forces them into categories. Begin by determining the range of population values:

mysql> SELECT MIN(pop), MAX(pop) FROM states; +----------+----------+ | MIN(pop) | MAX(pop) | +----------+----------+ |   506529 | 35893799 | +----------+----------+ 

You can see from that result that if you divide the pop values by five million, they'll group into six categoriesa reasonable number. (The category ranges will be 1 to 5,000,000, 5,000,001 to 10,000,000, and so forth.) To put each population value in the proper category, divide by five million, and use the integer result:

mysql> SELECT FLOOR(pop/5000000) AS 'max population (millions)',     -> COUNT(*) AS 'number of states'     -> FROM states GROUP BY 'max population (millions)'; +---------------------------+------------------+ | max population (millions) | number of states | +---------------------------+------------------+ |                         0 |               29 | |                         1 |               13 | |                         2 |                4 | |                         3 |                2 | |                         4 |                1 | |                         7 |                1 | +---------------------------+------------------+ 

Hmm. That's not quite right. The expression groups the population values into a small number of categories, all right, but doesn't report the category values properly. Let's try multiplying the FLOOR⁠(⁠ ⁠ ⁠) results by five:

mysql> SELECT FLOOR(pop/5000000)*5 AS 'max population (millions)',     -> COUNT(*) AS 'number of states'     -> FROM states GROUP BY 'max population (millions)'; +---------------------------+------------------+ | max population (millions) | number of states | +---------------------------+------------------+ |                         0 |               29 | |                         5 |               13 | |                        10 |                4 | |                        15 |                2 | |                        20 |                1 | |                        35 |                1 | +---------------------------+------------------+ 

That still isn't correct. The maximum state population was 35,893,799, which should go into a category for 40 million, not one for 35 million. The problem here is that the category-generating expression groups values toward the lower bound of each category. To group values toward the upper bound instead, use the following technique. For categories of size n, you can place a value x into the proper category using this expression:

FLOOR((x+(n-1))/n) 

So the final form of our query looks like this:

mysql> SELECT FLOOR((pop+4999999)/5000000)*5 AS 'max population (millions)',     -> COUNT(*) AS 'number of states'     -> FROM states GROUP BY 'max population (millions)'; +---------------------------+------------------+ | max population (millions) | number of states | +---------------------------+------------------+ |                         5 |               29 | |                        10 |               13 | |                        15 |                4 | |                        20 |                2 | |                        25 |                1 | |                        40 |                1 | +---------------------------+------------------+ 

The result shows clearly that the majority of U.S. states have a population of five million or less.

This technique works for all kinds of numeric values. For example, you can group mail table rows into categories of 100,000 bytes as follows:

mysql> SELECT FLOOR((size+99999)/100000) AS 'size (100KB)',     -> COUNT(*) AS 'number of messages'     -> FROM mail GROUP BY 'size (100KB)'; +--------------+--------------------+ | size (100KB) | number of messages | +--------------+--------------------+ |            1 |                 13 | |            2 |                  1 | |           10 |                  1 | |           24 |                  1 | +--------------+--------------------+ 

In some instances, it may be more appropriate to categorize groups on a logarithmic scale. For example, the state population values can be treated that way as follows:

mysql> SELECT FLOOR(LOG10(pop)) AS 'log10(population)',     -> COUNT(*) AS 'number of states'     -> FROM states GROUP BY 'log10(population)'; +-------------------+------------------+ | log10(population) | number of states | +-------------------+------------------+ |                 5 |                7 | |                 6 |               35 | |                 7 |                8 | +-------------------+------------------+ 

The query shows the number of states that have populations measured in hundreds of thousands, millions, and tens of millions, respectively.

How Repetitive Is a Set of Values?

To assess how much repetition is present in a set of values, use the ratio of COUNT(DISTINCT) and COUNT⁠(⁠ ⁠ ⁠). If all values are unique, both counts will be the same and the ratio will be 1. This is the case for the t values in the mail table and the pop values in the states table:

mysql> SELECT COUNT(DISTINCT t) / COUNT(t) FROM mail; +------------------------------+ | COUNT(DISTINCT t) / COUNT(t) | +------------------------------+ |                       1.0000 | +------------------------------+ mysql> SELECT COUNT(DISTINCT pop) / COUNT(pop) FROM states; +----------------------------------+ | COUNT(DISTINCT pop) / COUNT(pop) | +----------------------------------+ |                           1.0000 | +----------------------------------+ 

For a more repetitive set of values, COUNT(DISTINCT) will be less than COUNT⁠(⁠ ⁠ ⁠), and the ratio will be smaller:

mysql> SELECT COUNT(DISTINCT name) / COUNT(name) FROM driver_log; +------------------------------------+ | COUNT(DISTINCT name) / COUNT(name) | +------------------------------------+ |                             0.3000 | +------------------------------------+ 

What's the practical use for this ratio? A result close to zero indicates a high degree of repetition, which means the values will group into a small number of categories naturally. A result of 1 or close to it indicates many unique values, with the consequence that GROUP BY won't be very efficient for grouping the values into categories. (That is, there will be a lot of categories, relative to the number of values.) This tells you that to generate a summary, you'll probably find it necessary to impose an artificial categorization on the values, using the techniques described in this recipe.





MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net