Click here to Skip to main content
15,880,469 members
Articles / Database Development / SQL Server

Grouping Sets in T-SQL (SQL Server 2008)

Rate me:
Please Sign up or sign in to vote.
4.88/5 (23 votes)
13 Sep 2008CPOL2 min read 98.9K   21   8
Grouping Sets, a new feature in T-SQL in SQL Server 2008.

Introduction

This article discusses Grouping Sets in T-SQL. Grouping Sets is a new feature in T-SQL in SQL Server 2008.

Background

People cannot help appreciating the GROUP BY clause whenever they have to get a DISTINCT from any result set. Additionally, whenever any aggregate function is required, the GROUP BY clause is the only solution. There has always been a requirement get these aggregate functions based on different sets of columns in the same result set. It is also safe to use this feature as this is an ISO standard.

Though the same result could be achieved earlier, we would have to write different queries and would have to combine them using a UNION operator. The result set returned by a GROUPING SET is the union of the aggregates based on the columns specified in each set in the Grouping Set.

Example

To understand it completely, first we create a table tbl_Employee.

SQL
CREATE TABLE tbl_Employee 
( 
      Employee_Name varchar(25), 
      Region varchar(50), 
      Department varchar(40), 
      sal int 
)

Now, we populate the table with some of the following rows:

SQL
INSERT into tbl_Employee( 
                              Employee_Name, 
                              Region, 
                              Department, 
                              sal 
                        ) 

VALUES 

('Shujaat', 'North America', 'Information Technology', 9999), 
('Andrew', 'Asia Pacific', 'Information Technology',  5555), 
('Maria', 'North America', 'Human Resources', 4444), 
('Stephen', 'Middle East & Africa', 'Information Technology', 8888), 
('Stephen', 'Middle East & Africa', 'Human Resources', 8888)

After populating with the rows, we select some rows using Grouping Sets.

SQL
SELECT Region, Department, avg(sal) Average_Salary 
from tbl_Employee 
Group BY 
      GROUPING SETS 
      ( 
            (Region, Department), 
            (Region), 
            (Department) , 
            ()          
      )

The result of this statement is as follows:

Result.jpg

You can see that the result set contains rows grouped by each set in the specified Grouping Sets. You can see the average salary of employees for each region and department. You can also appreciate the average salary of employees for the organization (NULL for both Region and Department). This was the result of the empty Grouping Set, i.e., ().

Before 2008, if you had to get the same result set, the following query had to be written:

SQL
SELECT Region, Department, avg(sal) Average_Salary 
from tbl_Employee 
Group BY 
      Region, Department 
UNION 


SELECT Region, NULL, avg(sal) Average_Salary 
from tbl_Employee 

Group BY 
      Region 

UNION 

SELECT NULL, Department, avg(sal) Average_Salary 
from tbl_Employee 

Group BY 
      Department 

UNION 

SELECT NULL, NULL, avg(sal) Average_Salary 

from tbl_Employee

By looking at the above query, you can appreciate the ease provided by Grouping Sets to developers.

CUBE subclause for grouping

This is used to return the power n to 2 for n elements.

SQL
SELECT Region, Department, avg(sal) Average_Salary 
from tbl_Employee 
Group BY 
      CUBE (Region, Department)

The above query is equivalent to the following query:

SQL
SELECT Region, Department, avg(sal) Average_Salary 
from tbl_Employee 
GROUPING SETS 
      ( 
            (Region, Department), 
            (Region), 
            (Department) , 
            ()          
      )

ROLLUP subclause for grouping

This is used to return n+1 grouping sets for n elements in a hierarchy scenario.

SQL
SELECT Region, Department, avg(sal) Average_Salary 
from tbl_Employee 

Group BY 
      ROLLUP (Region, Department)

This is equivalent to the following query:

SQL
SELECT Region, Department, avg(sal) Average_Salary 
from tbl_Employee 

Group BY 
      Grouping Sets 
      ( 
            (Region, Department), 
            (Region), 
            () 
      )

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Muhammad Shujaat Siddiqi
New Jersey, USA

Comments and Discussions

 
PraiseVery easy understandebel explaining method Pin
Gor.Solomon20-Apr-16 22:02
Gor.Solomon20-Apr-16 22:02 
QuestionVery Easy to Understand Pin
Rehan Hussain4-Nov-13 18:35
Rehan Hussain4-Nov-13 18:35 
QuestionQuestion Pin
berrymaria10-Sep-13 15:53
berrymaria10-Sep-13 15:53 
AnswerRe: Question Pin
berrymaria10-Sep-13 15:57
berrymaria10-Sep-13 15:57 
GeneralMy vote of 4 Pin
Member 1000356323-Aug-13 12:49
Member 1000356323-Aug-13 12:49 
GeneralMy vote of 5 Pin
SoumyaJoe28-Feb-13 2:57
SoumyaJoe28-Feb-13 2:57 
GeneralMy vote of 5 Pin
Shreyash Gajbhiye20-Nov-12 9:25
professionalShreyash Gajbhiye20-Nov-12 9:25 
GeneralMy vote of 5 Pin
wujapan17-Oct-12 20:39
wujapan17-Oct-12 20:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.