content_image/704946.jpg content_image/704947.jpg content_image/704948.jpg content_image/704949.jpg
<img src='content_image/704946.jpg'>

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: 

<img src='content_image/704947.jpg'>

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: 

mysql> SELECT year, country, product, SUM(profit) AS profit FROM sales GROUP BY year, country, product WITH ROLLUP HAVING GROUPING(year, country, product) <> 0; 

<img src='content_image/704948.jpg'>

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” : 

mysql> SELECT * FROM t1; 

<img src='content_image/704949.jpg'>

