MySQL Cookbook Free Open Book

MySQL Cookbook

Previous Section Next Section

13.3 Per-Group Descriptive Statistics

13.3.1 Problem

You want to produce descriptive statistics for each subgroup of a set of observations.

13.3.2 Solution

Use aggregate functions, but employ a GROUP BY clause to arrange observations into the appropriate groups.

13.3.3 Discussion

The preceding section shows how to compute descriptive statistics for the entire set of scores in the testscore table. To be more specific, you can use GROUP BY to divide the observations into groups and calculate statistics for each of them. For example, the subjects in the testscore table are listed by age and sex, so it's possible to calculate similar statistics by age or sex (or both) by application of appropriate GROUP BY clauses.

By age:

mysql> SELECT age, COUNT(score) AS n,
    -> SUM(score) AS sum,
    -> MIN(score) AS minimum,
    -> MAX(score) AS maximum,
    -> AVG(score) AS mean,
    -> STD(score) AS 'std. dev.'
    -> FROM testscore
    -> GROUP BY age;
+-----+---+------+---------+---------+--------+-----------+
| age | n | sum  | minimum | maximum | mean   | std. dev. |
+-----+---+------+---------+---------+--------+-----------+
|   5 | 4 |   22 |       4 |       7 | 5.5000 |    1.1180 |
|   6 | 4 |   27 |       4 |       9 | 6.7500 |    1.9203 |
|   7 | 4 |   30 |       6 |       9 | 7.5000 |    1.1180 |
|   8 | 4 |   32 |       6 |      10 | 8.0000 |    1.5811 |
|   9 | 4 |   35 |       7 |      10 | 8.7500 |    1.0897 |
+-----+---+------+---------+---------+--------+-----------+

By sex:

mysql> SELECT sex, COUNT(score) AS n,
    -> SUM(score) AS sum,
    -> MIN(score) AS minimum,
    -> MAX(score) AS maximum,
    -> AVG(score) AS mean,
    -> STD(score) AS 'std. dev.'
    -> FROM testscore
    -> GROUP BY sex;
+-----+----+------+---------+---------+--------+-----------+
| sex | n  | sum  | minimum | maximum | mean   | std. dev. |
+-----+----+------+---------+---------+--------+-----------+
| M   | 10 |   71 |       4 |       9 | 7.1000 |    1.7000 |
| F   | 10 |   75 |       4 |      10 | 7.5000 |    1.8574 |
+-----+----+------+---------+---------+--------+-----------+

By age and sex:

mysql> SELECT age, sex, COUNT(score) AS n,
    -> SUM(score) AS sum,
    -> MIN(score) AS minimum,
    -> MAX(score) AS maximum,
    -> AVG(score) AS mean,
    -> STD(score) AS 'std. dev.'
    -> FROM testscore
    -> GROUP BY age, sex;
+-----+-----+---+------+---------+---------+--------+-----------+
| age | sex | n | sum  | minimum | maximum | mean   | std. dev. |
+-----+-----+---+------+---------+---------+--------+-----------+
|   5 | M   | 2 |    9 |       4 |       5 | 4.5000 |    0.5000 |
|   5 | F   | 2 |   13 |       6 |       7 | 6.5000 |    0.5000 |
|   6 | M   | 2 |   17 |       8 |       9 | 8.5000 |    0.5000 |
|   6 | F   | 2 |   10 |       4 |       6 | 5.0000 |    1.0000 |
|   7 | M   | 2 |   14 |       6 |       8 | 7.0000 |    1.0000 |
|   7 | F   | 2 |   16 |       7 |       9 | 8.0000 |    1.0000 |
|   8 | M   | 2 |   15 |       6 |       9 | 7.5000 |    1.5000 |
|   8 | F   | 2 |   17 |       7 |      10 | 8.5000 |    1.5000 |
|   9 | M   | 2 |   16 |       7 |       9 | 8.0000 |    1.0000 |
|   9 | F   | 2 |   19 |       9 |      10 | 9.5000 |    0.5000 |
+-----+-----+---+------+---------+---------+--------+-----------+
    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Preface
    Chapter 1. Using the mysql Client Program
    Chapter 2. Writing MySQL-Based Programs
    Chapter 3. Record Selection Techniques
    Chapter 4. Working with Strings
    Chapter 5. Working with Dates and Times
    Chapter 6. Sorting Query Results
    Chapter 7. Generating Summaries
    Chapter 8. Modifying Tables with ALTER TABLE
    Chapter 9. Obtaining and Using Metadata
    Chapter 10. Importing and Exporting Data
    Chapter 11. Generating and Using Sequences
    Chapter 12. Using Multiple Tables
    Chapter 13. Statistical Techniques
    13.1 Introduction
    13.2 Calculating Descriptive Statistics
    13.3 Per-Group Descriptive Statistics
    13.4 Generating Frequency Distributions
    13.5 Counting Missing Values
    13.6 Calculating Linear Regressions or Correlation Coefficients
    13.7 Generating Random Numbers
    13.8 Randomizing a Set of Rows
    13.9 Selecting Random Items from a Set of Rows
    13.10 Assigning Ranks
    Chapter 14. Handling Duplicates
    Chapter 15. Performing Transactions
    Chapter 16. Introduction to MySQL on the Web
    Chapter 17. Incorporating Query Resultsinto Web Pages
    Chapter 18. Processing Web Input with MySQL
    Chapter 19. Using MySQL-Based Web Session Management
    Appendix A. Obtaining MySQL Software
    Appendix B. JSP and Tomcat Primer
    Appendix C. References
    Colophone
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele