we will discuss the use of Grouping function in SQL Server.
We will use the following Sales table for this example.
![sql server rollup grouping](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9XA_2Dwelxz56svPNPO2ZA9NDwAP1mv_c6FCmNGhXHsjgsGQIT0uSaUvyLd8DMm1j6SRrJbQvIc5RcrsMpPfIfGvbHal5HIXVqRGAmjgPpAYl3rHSCxzwEbp20ibUn2BPvf1aOro5qd4/s1600/sql+server+rollup+grouping.png)
What is Grouping function
Grouping(Column) indicates whether the column in a GROUP BY list is aggregated or not. Grouping returns 1 for aggregated or 0 for not aggregated in the result set.
The following query returns 1 for aggregated or 0 for not aggregated in the result set
SELECT Continent, Country, City, SUM(SaleAmount) AS TotalSales,
GROUPING(Continent) AS GP_Continent,
GROUPING(Country) AS GP_Country,
GROUPING(City) AS GP_City
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
Result :
![Grouping function in SQL Server](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjG8GSGluw6MWmLcnaFu5yAEkEEggcfEandlxFv3sNDWkC5adzGaVZlE1KEEbK4RNxKTlNA9tb8rE7pTTcPmqP6cK2QuTI5AdSLzOrQedQdR9P2gY6zfiftw4DWqmZYoSBQiGqaiHVAaJw/s1600/Grouping+function+in+SQL+Server.png)
What is the use of Grouping function in real world
When a column is aggregated in the result set, the column will have a NULL value. If you want to replace NULL with All then this GROUPING function is very handy.
SELECT
CASE WHEN
GROUPING(Continent) = 1 THEN 'All' ELSE ISNULL(Continent, 'Unknown')
END AS Continent,
CASE WHEN
GROUPING(Country) = 1 THEN 'All' ELSE ISNULL(Country, 'Unknown')
END AS Country,
CASE
WHEN GROUPING(City) = 1 THEN 'All' ELSE ISNULL(City, 'Unknown')
END AS City,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
Result :
![sql server rollup replace null](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgnCgbBQw3LZ1e_9OiuENxTvvmHC5AMVKCUFn6fqt7NQj2eWGD2oxM97RMQwQiaJwn3PvtF1StDsy2f1zVhFLQGTmCN0SEqlN7wp38OSPUZL8UlT0J-GoaV-S7-7D9scvtQRLGux3oRXNY/s1600/sql+server+rollup+replace+null.png)
Can't I use ISNULL function instead as shown below
SELECT ISNULL(Continent, 'All') AS Continent,
ISNULL(Country, 'All') AS Country,
ISNULL(City, 'All') AS City,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
Well, you can, but only if your data does not contain NULL values. Let me explain what I mean.
At the moment the raw data in our Sales has no NULL values. Let's introduce a NULL value in the City column of the row where Id = 1
Update Sales Set City = NULL where Id = 1
Now execute the following query with ISNULL function
SELECT ISNULL(Continent, 'All') AS Continent,
ISNULL(Country, 'All') AS Country,
ISNULL(City, 'All') AS City,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
Result : Notice that the actuall NULL value in the raw data is also replaced with the word 'All', which is incorrect. Hence the need for Grouping function.
![sql server rollup grouping replace null](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQ_CQoPzY6-ty47Xz-cZ-P9fbWNz_G19SuZW1esetcM6b5EceeqiuRh1dRs6oua-RcowOW0iUj2xxa7mThHPbU_Fq8GDOrM2oNjag4Wb-4taiKSbvE9H9Y3GQmK_lnoF6zF5YQZUJdb5Q/s1600/sql+server+rollup+grouping+replace+null.png)
Please note : Grouping function can be used with Rollup, Cube and Grouping Sets
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.