In this tutorial, we will use the same stock table which we have used in the ROLLUP AND CUBE tutorial

The SQL GROUPING Functions is used to indicate whether the specified column in a GROUP BY Clause is aggregated or not. It will return one for aggregated and zero for not aggregated.

The following query will return 1 for aggregated and 0 for not aggregated in result set for the columns GP_ProductName and GP_model
SELECT ProductName,model,SUM(Quantity) Quantity,
GROUPING(ProductName) GP_ProductName,
GROUPING(model) GP_model
FROM Stock
GROUP BY ProductName,model WITH ROLLUP
Result:-

Real-world scenario for GROUPING function
By default ROLLUP and CUBE operators in SQL provide NULL values for total and subtotals so by using the GROUPING function you can replace NULL value by "ALL" as given in the below query

1. GROUPING function usage with ROLLUP Operator in Real-world.
SELECT ProductName,model,SUM(Quantity) Quantity,
GROUPING(ProductName) Grouping_ProductName,
GROUPING(model) Grouping_model
FROM Stock
GROUP BY ProductName,model WITH ROLLUP

2. GROUPING function usage with ROLLUP Operator in Real-world.
SELECT CASE WHEN GROUPING(ProductName)=1 THEN 'All' ELSE ProductName END ProductName,
CASE WHEN GROUPING(Godown)=1 THEN 'All' ELSE Godown END Godown,
SUM(Quantity) Quantity 
FROM Stock
GROUP BY ProductName,Godown WITH CUBE