grouping_id returns a number corresponding to the GROUPING bit vector associated with a row. grouping_id is applicable only in a SELECT statement that contains a GROUP BY extension, such as ROLLUP or CUBE, and a grouping function. In queries with many GROUP BY expressions, determining the GROUP BY level of a particular row requires many grouping functions, which leads to cumbersome SQL. grouping_id is useful in these cases.
grouping_id is functionally equivalent to taking the results of multiple grouping functions and concatenating them into a bit vector (a string of ones and zeros). By using grouping_id you can avoid the need for multiple grouping functions and make row filtering conditions easier to express. Row filtering is easier with grouping_id because the desired rows can be identified with a single condition of grouping_id = n. The function is especially useful when storing multiple levels of aggregation in a single table.