Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My Experiments with SQLServer
Browse by Tags · View All
SQLServer 126
SQLServer 2008 R2 91
SQLServer 2008 86
SQLServer 2005 82
SQL 60
Database 59
Sql And Me 57
Tips & Tricks 28
SQL Server 27
SQL FAQ 26

Archive · View All
May 2011 25
June 2011 21
July 2011 21
August 2011 19
April 2011 16
January 2013 4
May 2012 3
April 2012 3
October 2011 3
February 2013 2

Vishal Gajjar's Blog

SQL Server – TSQL – GROUP BY GROUPING SETS()

Jul 12 2011 8:00AM by Vishal Gajjar   

By using GROUPING SETS() we can specify multiple groupings in a single query. GROUPING SETS() generates the result by producing a UNION ALL set of the result sets generated by specified grouping sets.

for example, consider below data:

-- © 2011 – Vishal (http://SqlAndMe.com)
 
SELECT      [Group], [Name], [SalesYTD]
FROM        dbo.Sales

Result Set:

Group                   Name              SalesYTD
—————————— 		        ——————————        —————————————
North America           Northwest         123237.00
South America           Northwest         37534.00
South America           Southwest         39667.00
North America           Southwest         164232.00
 
(4 row(s) affected)

To summarize this data we can use GROUP BY, to summarize by more that one set we need multiple SELECTs, and UNION ALL to merge the result set:

SELECT      [Group], [Name], SUM(SalesYTD) AS 'Total Sales'
FROM        dbo.Sales
GROUP BY [Group], [Name]
 
UNION ALL
 
SELECT      [Group], NULL, SUM(SalesYTD) AS 'Total Sales'
FROM        dbo.Sales
GROUP BY [Group]
 
UNION ALL
 
SELECT      NULL, NULL, SUM(SalesYTD) AS 'Total Sales'
FROM        dbo.Sales

Result Set:

Group                     Name                 Total Sales
——————————                ——————————           —————————————
North America             Northwest            123237.00
South America             Northwest            37534.00
North America             Southwest            164232.00
South America             Southwest            39667.00
North America             NULL                 287469.00
South America             NULL                 77201.00
NULL                      NULL                 364670.00
 
(7 row(s) affected)

The above result set can be achieved by a single GROUPING SETS() clause as below:

– WITH ROLLUP Equivalent
SELECT      [Group], [Name], SUM(SalesYTD) AS 'Total Sales'
FROM        dbo.Sales
GROUP BY GROUPING SETS (([Group], [Name]), ([Group]), ())

As the sets we have specified generate sub-total and a grand-total row, the same result set can be produced by using GROUP BY ROLLUP().

The empty set ‘( )’ in GROUPING SETS() generates grand total.

GROUPING SETS() can also be used to generate results produced by GROUP BY CUBE(), since we can specify all the sets generated by GROUP BY CUBE().

SELECT      [Group], [Name], SUM(SalesYTD) AS 'Total Sales'
FROM        dbo.Sales
GROUP BY CUBE ([Group], [Name])
 
– WITH CUBE Equivalent
SELECT      [Group], [Name], SUM(SalesYTD) AS 'Total Sales'
FROM        dbo.Sales
GROUP BY GROUPING SETS (([Group], [Name]), ([Group]), ([Name]), ())

Result Set (2nd):

Group                    Name                Total Sales
——————————               ——————————          —————————————
North America            Northwest           123237.00
South America            Northwest           37534.00
NULL                     Northwest           160771.00
North America            Southwest           164232.00
South America            Southwest           39667.00
NULL                     Southwest           203899.00
NULL                     NULL                364670.00
North America            NULL                287469.00
South America            NULL                77201.00
 
(9 row(s) affected)

Let’s take another example, if you need to generate a pivot table based on sales data, you will need to group data by multiple sets:

To generate this pivot, we need to group results by four different sets, This can be written using T-SQL as below:

-- Equivalent to GROUP BY CUBE
SELECT      [Product], [Year], SUM(SalesYTD) AS 'Total Sales'
FROM        dbo.ProductSales
GROUP BY GROUPING SETS
(     ([Product], [Year]),
      ([Product]),
      ([Year]),
      ()
)

Hope This Helps! Cheers!


Republished from Sql&Me [31 clicks].  Read the original version here [32134 clicks].

Vishal Gajjar
46 · 4% · 1276
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]