The GROUPING() Function
Expand your knowledge on GROUP BY WITH ROLLUP with the GROUPING modifier.
We'll cover the following...
MySQL provides a WITH ROLLUP modifier for a GROUP BY clause in a SELECT statement. The modifier that functions as a suffix to the GROUP BY’s list of expressions yields aggregate rows for the groups created through GROUP BY. This can be very helpful in scenarios where we would otherwise need to formulate a second query to retrieve the same information. A great example of this is the summary of car parts built by type and date:
Noticeably, we exclude rows where built_at is NULL with the corresponding WHERE clause (line 10). That is, we do not consider car parts that have not been built, yet. While this makes sense, the information on car parts that have not been built is valuable, nonetheless:
However, including this information in the statement’s output leaves us with a problem:
How do we know ...