The SQL GROUPING is one of the SQL Aggregate Function, which is used to indicate whether the specified column in a GROUP BY Clause is aggregated or not. This function will return one for aggregated, and zero for not aggregated.
SQL Grouping Function Syntax
The basic syntax behind this Grouping in SQL Server is as shown below:
SELECT GROUPING ([Column_Name]) FROM [Source] GROUP BY [Column_Name]
In this article we will show you, How to use the grouping in SQL Server with example. For this, We are going to use the [MyEmployee table] present in our [SQL Tutorial] Database. From the below figure you can observe that, [MyEmployee table] table have fourteen records
SQL Grouping Function Example
The Grouping in SQL Server simply returns whether the grouping on the specified column is happens or not, by returning 1, and 0. In this example, we will show you the same.
-- GROUPING in SQL Server USE [SQL Tutorial] GO SELECT [Education] ,[Occupation] ,GROUPING([Occupation]) AS 'Grouping' ,GROUPING([Education]) AS 'Grouping 2' ,SUM([YearlyIncome]) as income FROM [MyEmployees Table] GROUP BY [Education] ,[Occupation] WITH ROLLUP
Following statements will check whether the Grouping is performed on the Education, and Occupation columns ar not by returning 1 and 0
,GROUPING([Occupation]) AS 'Grouping' ,GROUPING([Education]) AS 'Grouping 2'
TIP: Please refer SQL GROUP BY Clause to understand the Grouping techniques.
Thank You for Visiting Our Blog