Version sans cache

Mise en cache désactivé. Réglage défaut pour cette page : actif (code DEF204)
Si l'affichage est trop lent, vous pouvez désactiver le mode utilisateur pour visualiser la version en cache.

Rechercher dans le manuel MySQL

12.20.2 GROUP BY Modifiers

The GROUP BY clause permits a WITH ROLLUP modifier that causes summary output to include extra rows that represent higher-level (that is, super-aggregate) summary operations. ROLLUP thus enables you to answer questions at multiple levels of analysis with a single query. For example, ROLLUP can be used to provide support for OLAP (Online Analytical Processing) operations.

Suppose that a sales table has year, country, product, and profit columns for recording sales profitability:

  1. (
  2.     year    INT,
  3.     country VARCHAR(20),
  4.     product VARCHAR(32),
  5.     profit  INT
  6. );

To summarize table contents per year, use a simple GROUP BY like this:

  1. mysql> SELECT year, SUM(profit) AS profit
  2.        FROM sales
  3.        GROUP BY year;
  4. +------+--------+
  5. | year | profit |
  6. +------+--------+
  7. | 2000 |   4525 |
  8. | 2001 |   3010 |
  9. +------+--------+

The output shows the total (aggregate) profit for each year. To also determine the total profit summed over all years, you must add up the individual values yourself or run an additional query. Or you can use ROLLUP, which provides both levels of analysis with a single query. Adding a WITH ROLLUP modifier to the GROUP BY clause causes the query to produce another (super-aggregate) row that shows the grand total over all year values:

  1. mysql> SELECT year, SUM(profit) AS profit
  2.        FROM sales
  3. +------+--------+
  4. | year | profit |
  5. +------+--------+
  6. | 2000 |   4525 |
  7. | 2001 |   3010 |
  8. | NULL |   7535 |
  9. +------+--------+

The NULL value in the year column identifies the grand total super-aggregate line.

ROLLUP has a more complex effect when there are multiple GROUP BY columns. In this case, each time there is a change in value in any but the last grouping column, the query produces an extra super-aggregate summary row.

For example, without ROLLUP, a summary of the sales table based on year, country, and product might look like this, where the output indicates summary values only at the year/country/product level of analysis:

  1. mysql> SELECT year, country, product, SUM(profit) AS profit
  2.        FROM sales
  3.        GROUP BY year, country, product;
  4. +------+---------+------------+--------+
  5. | year | country | product    | profit |
  6. +------+---------+------------+--------+
  7. | 2000 | Finland | Computer   |   1500 |
  8. | 2000 | Finland | Phone      |    100 |
  9. | 2000 | India   | Calculator |    150 |
  10. | 2000 | India   | Computer   |   1200 |
  11. | 2000 | USA     | Calculator |     75 |
  12. | 2000 | USA     | Computer   |   1500 |
  13. | 2001 | Finland | Phone      |     10 |
  14. | 2001 | USA     | Calculator |     50 |
  15. | 2001 | USA     | Computer   |   2700 |
  16. | 2001 | USA     | TV         |    250 |
  17. +------+---------+------------+--------+

With ROLLUP added, the query produces several extra rows:

  1. mysql> SELECT year, country, product, SUM(profit) AS profit
  2.        FROM sales
  3.        GROUP BY year, country, product WITH ROLLUP;
  4. +------+---------+------------+--------+
  5. | year | country | product    | profit |
  6. +------+---------+------------+--------+
  7. | 2000 | Finland | Computer   |   1500 |
  8. | 2000 | Finland | Phone      |    100 |
  9. | 2000 | Finland | NULL       |   1600 |
  10. | 2000 | India   | Calculator |    150 |
  11. | 2000 | India   | Computer   |   1200 |
  12. | 2000 | India   | NULL       |   1350 |
  13. | 2000 | USA     | Calculator |     75 |
  14. | 2000 | USA     | Computer   |   1500 |
  15. | 2000 | USA     | NULL       |   1575 |
  16. | 2000 | NULL    | NULL       |   4525 |
  17. | 2001 | Finland | Phone      |     10 |
  18. | 2001 | Finland | NULL       |     10 |
  19. | 2001 | USA     | Calculator |     50 |
  20. | 2001 | USA     | Computer   |   2700 |
  21. | 2001 | USA     | TV         |    250 |
  22. | 2001 | USA     | NULL       |   3000 |
  23. | 2001 | NULL    | NULL       |   3010 |
  24. | NULL | NULL    | NULL       |   7535 |
  25. +------+---------+------------+--------+

Now the output includes summary information at four levels of analysis, not just one:

  • Following each set of product rows for a given year and country, an extra super-aggregate summary row appears showing the total for all products. These rows have the product column set to NULL.

  • Following each set of rows for a given year, an extra super-aggregate summary row appears showing the total for all countries and products. These rows have the country and products columns set to NULL.

  • Finally, following all other rows, an extra super-aggregate summary row appears showing the grand total for all years, countries, and products. This row has the year, country, and products columns set to NULL.

The NULL indicators in each super-aggregate row are produced when the row is sent to the client. The server looks at the columns named in the GROUP BY clause following the leftmost one that has changed value. For any column in the result set with a name that matches any of those names, its value is set to NULL. (If you specify grouping columns by column position, the server identifies which columns to set to NULL by position.)

Because the NULL values in the super-aggregate rows are placed into the result set at such a late stage in query processing, you can test them as NULL values only in the select list or HAVING clause. You cannot test them as NULL values in join conditions or the WHERE clause to determine which rows to select. For example, you cannot add WHERE product IS NULL to the query to eliminate from the output all but the super-aggregate rows.

The NULL values do appear as NULL on the client side and can be tested as such using any MySQL client programming interface. However, at this point, you cannot distinguish whether a NULL represents a regular grouped value or a super-aggregate value. To test the distinction, use the GROUPING() function, described later.

Previously, MySQL did not allow the use of DISTINCT or ORDER BY in a query having a WITH ROLLUP option. This restriction is lifted in MySQL 8.0.12 and later. (Bug #87450, Bug #86311, Bug #26640100, Bug #26073513)

For GROUP BY ... WITH ROLLUP queries, to test whether NULL values in the result represent super-aggregate values, the GROUPING() function is available for use in the select list, HAVING clause, and (as of MySQL 8.0.12) ORDER BY clause. For example, GROUPING(year) returns 1 when NULL in the year column occurs in a super-aggregate row, and 0 otherwise. Similarly, GROUPING(country) and GROUPING(product) return 1 for super-aggregate NULL values in the country and product columns, respectively:

  1. mysql> SELECT
  2.          year, country, product, SUM(profit) AS profit,
  3.          GROUPING(year) AS grp_year,
  4.          GROUPING(country) AS grp_country,
  5.          GROUPING(product) AS grp_product
  6.        FROM sales
  7.        GROUP BY year, country, product WITH ROLLUP;
  8. +------+---------+------------+--------+----------+-------------+-------------+
  9. | year | country | product    | profit | grp_year | grp_country | grp_product |
  10. +------+---------+------------+--------+----------+-------------+-------------+
  11. | 2000 | Finland | Computer   |   1500 |        0 |           0 |           0 |
  12. | 2000 | Finland | Phone      |    100 |        0 |           0 |           0 |
  13. | 2000 | Finland | NULL       |   1600 |        0 |           0 |           1 |
  14. | 2000 | India   | Calculator |    150 |        0 |           0 |           0 |
  15. | 2000 | India   | Computer   |   1200 |        0 |           0 |           0 |
  16. | 2000 | India   | NULL       |   1350 |        0 |           0 |           1 |
  17. | 2000 | USA     | Calculator |     75 |        0 |           0 |           0 |
  18. | 2000 | USA     | Computer   |   1500 |        0 |           0 |           0 |
  19. | 2000 | USA     | NULL       |   1575 |        0 |           0 |           1 |
  20. | 2000 | NULL    | NULL       |   4525 |        0 |           1 |           1 |
  21. | 2001 | Finland | Phone      |     10 |        0 |           0 |           0 |
  22. | 2001 | Finland | NULL       |     10 |        0 |           0 |           1 |
  23. | 2001 | USA     | Calculator |     50 |        0 |           0 |           0 |
  24. | 2001 | USA     | Computer   |   2700 |        0 |           0 |           0 |
  25. | 2001 | USA     | TV         |    250 |        0 |           0 |           0 |
  26. | 2001 | USA     | NULL       |   3000 |        0 |           0 |           1 |
  27. | 2001 | NULL    | NULL       |   3010 |        0 |           1 |           1 |
  28. | NULL | NULL    | NULL       |   7535 |        1 |           1 |           1 |
  29. +------+---------+------------+--------+----------+-------------+-------------+

Instead of displaying the GROUPING() results directly, you can use GROUPING() to substitute labels for super-aggregate NULL values:

  1. mysql> SELECT
  2.          IF(GROUPING(year), 'All years', year) AS year,
  3.          IF(GROUPING(country), 'All countries', country) AS country,
  4.          IF(GROUPING(product), 'All products', product) AS product,
  5.          SUM(profit) AS profit
  6.        FROM sales
  7.        GROUP BY year, country, product WITH ROLLUP;
  8. +-----------+---------------+--------------+--------+
  9. | year      | country       | product      | profit |
  10. +-----------+---------------+--------------+--------+
  11. | 2000      | Finland       | Computer     |   1500 |
  12. | 2000      | Finland       | Phone        |    100 |
  13. | 2000      | Finland       | All products |   1600 |
  14. | 2000      | India         | Calculator   |    150 |
  15. | 2000      | India         | Computer     |   1200 |
  16. | 2000      | India         | All products |   1350 |
  17. | 2000      | USA           | Calculator   |     75 |
  18. | 2000      | USA           | Computer     |   1500 |
  19. | 2000      | USA           | All products |   1575 |
  20. | 2000      | All countries | All products |   4525 |
  21. | 2001      | Finland       | Phone        |     10 |
  22. | 2001      | Finland       | All products |     10 |
  23. | 2001      | USA           | Calculator   |     50 |
  24. | 2001      | USA           | Computer     |   2700 |
  25. | 2001      | USA           | TV           |    250 |
  26. | 2001      | USA           | All products |   3000 |
  27. | 2001      | All countries | All products |   3010 |
  28. | All years | All countries | All products |   7535 |
  29. +-----------+---------------+--------------+--------+

With multiple expression arguments, GROUPING() returns a result representing a bitmask the combines the results for each expression, with the lowest-order bit corresponding to the result for the rightmost expression. For example, GROUPING(year, country, product) is evaluated like this:

  result for GROUPING(product)
+ result for GROUPING(country) << 1
+ result for GROUPING(year) << 2

The result of such a GROUPING() is nonzero if any of the expressions represents a super-aggregate NULL, so you can return only the super-aggregate rows and filter out the regular grouped rows like this:

  1. mysql> SELECT year, country, product, SUM(profit) AS profit
  2.        FROM sales
  3.        GROUP BY year, country, product WITH ROLLUP
  4.        HAVING GROUPING(year, country, product) <> 0;
  5. +------+---------+---------+--------+
  6. | year | country | product | profit |
  7. +------+---------+---------+--------+
  8. | 2000 | Finland | NULL    |   1600 |
  9. | 2000 | India   | NULL    |   1350 |
  10. | 2000 | USA     | NULL    |   1575 |
  11. | 2000 | NULL    | NULL    |   4525 |
  12. | 2001 | Finland | NULL    |     10 |
  13. | 2001 | USA     | NULL    |   3000 |
  14. | 2001 | NULL    | NULL    |   3010 |
  15. | NULL | NULL    | NULL    |   7535 |
  16. +------+---------+---------+--------+

The sales table contains no NULL values, so all NULL values in a ROLLUP result represent super-aggregate values. When the data set contains NULL values, ROLLUP summaries may contain NULL values not only in super-aggregate rows, but also in regular grouped rows. GROUPING() enables these to be distinguished. Suppose that table t1 contains a simple data set with two grouping factors for a set of quantity values, where NULL indicates something like other or unknown:

  1. mysql> SELECT * FROM t1;
  2. +------+-------+----------+
  3. | name | size  | quantity |
  4. +------+-------+----------+
  5. | ball | small |       10 |
  6. | ball | large |       20 |
  7. | ball | NULL  |        5 |
  8. | hoop | small |       15 |
  9. | hoop | large |        5 |
  10. | hoop | NULL  |        3 |
  11. +------+-------+----------+

A simple ROLLUP operation produces these results, in which it is not so easy to distinguish NULL values in super-aggregate rows from NULL values in regular grouped rows:

  1. mysql> SELECT name, size, SUM(quantity) AS quantity
  2.        FROM t1
  3.        GROUP BY name, size WITH ROLLUP;
  4. +------+-------+----------+
  5. | name | size  | quantity |
  6. +------+-------+----------+
  7. | ball | NULL  |        5 |
  8. | ball | large |       20 |
  9. | ball | small |       10 |
  10. | ball | NULL  |       35 |
  11. | hoop | NULL  |        3 |
  12. | hoop | large |        5 |
  13. | hoop | small |       15 |
  14. | hoop | NULL  |       23 |
  15. | NULL | NULL  |       58 |
  16. +------+-------+----------+

Using GROUPING() to substitute labels for the super-aggregate NULL values makes the result easier to interpret:

  1. mysql> SELECT
  2.          IF(GROUPING(name) = 1, 'All items', name) AS name,
  3.          IF(GROUPING(size) = 1, 'All sizes', size) AS size,
  4.          SUM(quantity) AS quantity
  5.        FROM t1
  6.        GROUP BY name, size WITH ROLLUP;
  7. +-----------+-----------+----------+
  8. | name      | size      | quantity |
  9. +-----------+-----------+----------+
  10. | ball      | NULL      |        5 |
  11. | ball      | large     |       20 |
  12. | ball      | small     |       10 |
  13. | ball      | All sizes |       35 |
  14. | hoop      | NULL      |        3 |
  15. | hoop      | large     |        5 |
  16. | hoop      | small     |       15 |
  17. | hoop      | All sizes |       23 |
  18. | All items | All sizes |       58 |
  19. +-----------+-----------+----------+

Other Considerations When using ROLLUP

The following discussion lists some behaviors specific to the MySQL implementation of ROLLUP.

Prior to MySQL 8.0.12, when you use ROLLUP, you cannot also use an ORDER BY clause to sort the results. In other words, ROLLUP and ORDER BY were mutually exclusive in MySQL. However, you still have some control over sort order. To work around the restriction that prevents using ROLLUP with ORDER BY and achieve a specific sort order of grouped results, generate the grouped result set as a derived table and apply ORDER BY to it. For example:

  1. mysql> SELECT * FROM
  2.          (SELECT year, SUM(profit) AS profit
  3.          FROM sales GROUP BY year WITH ROLLUP) AS dt
  4.        ORDER BY year DESC;
  5. +------+--------+
  6. | year | profit |
  7. +------+--------+
  8. | 2001 |   3010 |
  9. | 2000 |   4525 |
  10. | NULL |   7535 |
  11. +------+--------+

As of MySQL 8.0.12, ORDER BY and ROLLUP can be used together, which enables the use of ORDER BY and GROUPING() to achieve a specific sort order of grouped results. For example:

  1. mysql> SELECT year, SUM(profit) AS profit
  2.        FROM sales
  3.        ORDER BY GROUPING(year) DESC;
  4. +------+--------+
  5. | year | profit |
  6. +------+--------+
  7. | NULL |   7535 |
  8. | 2000 |   4525 |
  9. | 2001 |   3010 |
  10. +------+--------+

In both cases, the super-aggregate summary rows sort with the rows from which they are calculated, and their placement depends on sort order (at the end for ascending sort, at the beginning for descending sort).

LIMIT can be used to restrict the number of rows returned to the client. LIMIT is applied after ROLLUP, so the limit applies against the extra rows added by ROLLUP. For example:

  1. mysql> SELECT year, country, product, SUM(profit) AS profit
  2.        FROM sales
  3.        GROUP BY year, country, product WITH ROLLUP
  4.        LIMIT 5;
  5. +------+---------+------------+--------+
  6. | year | country | product    | profit |
  7. +------+---------+------------+--------+
  8. | 2000 | Finland | Computer   |   1500 |
  9. | 2000 | Finland | Phone      |    100 |
  10. | 2000 | Finland | NULL       |   1600 |
  11. | 2000 | India   | Calculator |    150 |
  12. | 2000 | India   | Computer   |   1200 |
  13. +------+---------+------------+--------+

Using LIMIT with ROLLUP may produce results that are more difficult to interpret, because there is less context for understanding the super-aggregate rows.

A MySQL extension permits a column that does not appear in the GROUP BY list to be named in the select list. (For information about nonaggregated columns and GROUP BY, see Section 12.20.3, “MySQL Handling of GROUP BY”.) In this case, the server is free to choose any value from this nonaggregated column in summary rows, and this includes the extra rows added by WITH ROLLUP. For example, in the following query, country is a nonaggregated column that does not appear in the GROUP BY list and values chosen for this column are nondeterministic:

  1. mysql> SELECT year, country, SUM(profit) AS profit
  2.        FROM sales
  3. +------+---------+--------+
  4. | year | country | profit |
  5. +------+---------+--------+
  6. | 2000 | India   |   4525 |
  7. | 2001 | USA     |   3010 |
  8. | NULL | USA     |   7535 |
  9. +------+---------+--------+

This behavior is permitted when the ONLY_FULL_GROUP_BY SQL mode is not enabled. If that mode is enabled, the server rejects the query as illegal because country is not listed in the GROUP BY clause. With ONLY_FULL_GROUP_BY enabled, you can still execute the query by using the ANY_VALUE() function for nondeterministic-value columns:

  1. mysql> SELECT year, ANY_VALUE(country) AS country, SUM(profit) AS profit
  2.        FROM sales
  3. +------+---------+--------+
  4. | year | country | profit |
  5. +------+---------+--------+
  6. | 2000 | India   |   4525 |
  7. | 2001 | USA     |   3010 |
  8. | NULL | USA     |   7535 |
  9. +------+---------+--------+

Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-group-by-modifiers.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut