- 10.1 Introduction
- 10.2 Grouping on One Column
- 10.3 Grouping on Two or More Columns
- 10.4 Grouping on Expressions
- 10.5 Grouping of Null Values
- 10.6 Grouping with Sorting
- 10.7 General Rules for the GROUP BY Clause
- 10.8 The GROUP_CONCAT Function
- 10.9 Complex Examples with GROUP BY
- 10.10 Grouping with WITH ROLLUP
- 10.11 Answers
10.10 Grouping with WITH ROLLUP
The GROUP BY clause has many features to group data and calculate aggregated data, such as the total number of penalties or the sum of all penalties. However, all statements return results in which all data is on the same level of aggregation. But what if we want to see data belonging to different aggregation levels within one statement? Imagine that with one statement we want to see the total penalty amount for each player, followed by the total penalty amount for all players. The forms of the GROUP BY clauses discussed so far do not make this possible. To achieve the desired result, more than two groupings within one GROUP BY clause are required. By adding the specification WITH ROLLUP to the GROUP BY clause, it becomes possible.
Example 10.23: For each player, find the sum of all his or her penalties, plus the sum of all penalties.
Use the UNION operator as a way to combine these two groupings into one statement.
SELECT PLAYERNO, SUM(AMOUNT) FROM PENALTIES GROUP BY PLAYERNO UNION SELECT NULL, SUM(AMOUNT) FROM PENALTIES
The result is:
PLAYERNO SUM(AMOUNT) -------- ----------- 6 100.00 8 25.00 27 175.00 44 130.00 104 50.00 ? 480.00
Explanation: The rows of this intermediate result in which the PLAYERNO column is filled form the result of the first select block. The rows in which PLAYERNO is equal to null make up the result of the second select block. The first five rows contain data on the aggregation level of the player numbers, and the last row contains data on the aggregation level of all rows.
The specification WITH ROLLUP has been introduced to simplify this kind of statement. WITH ROLLUP can be used to ask for multiple groupings with one GROUP BY clause. Using this approach, the previous statement would then be
SELECT PLAYERNO, SUM(AMOUNT) FROM PENALTIES GROUP BY PLAYERNO WITH ROLLUP
Explanation: The result of this statement is the same as the previous one. The specification WITH ROLLUP indicates that after the result has been grouped on [PLAYERNO], another grouping is needed—in this case, on all rows.
To further define this concept, imagine that the expressions E1, E2, E3, and E4 are specified in a GROUP BY clause. The grouping performed is [E1, E2, E3, E4]. When we add the specification WITH ROLLUP to this GROUP BY clause, an entire set of groupings is performed: [E1, E2, E3, E4], [E1, E2, E3], [E1, E2], [E1], and finally []. The specification [] means that all rows are grouped into one group. The specified grouping is seen as the highest aggregation level that is asked and also indicates that all higher aggregation levels must be calculated again. To aggregate upward is called rollup. So the result of this statement contains data on five different levels of aggregation.
If an expression occurs in the SELECT clause in which the result of a certain grouping is not grouped, the null value is placed in the result.
Example 10.24: For each combination of sex-town, get the number of players, total number of players per sex, and total number of players in the entire table.
SELECT SEX, TOWN, COUNT(*) FROM PLAYERS GROUP BY SEX, TOWN WITH ROLLUP
The result is:
SEX TOWN COUNT(*) --- --------- -------- M Stratford 7 M Inglewood 1 M Douglas 1 M ? 9 F Midhurst 1 F Inglewood 1 F Plymouth 1 F Eltham 2 F ? 5 ? ? 14
Explanation: This result has three levels of aggregation. Rows 1, 2, 3, 5, 6, 7, and 8 form the lowest level and have been added because of the grouping [SEX, TOWN]; rows 4 and 9 have been added because of the grouping [SEX]; and the last row forms the highest level of aggregation and has been added because of the grouping []. It contains the total number of players.
Exercise 10.20: For each team, get the number of matches played and also the total number of matches.
Exercise 10.21: Group the matches by the name of the player and division of the team, and execute a ROLLUP. Then for each group, get the name of the player, division of the team, and total number of sets won.