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 WITH ROLLUP

Jul 7 2011 8:25AM by Vishal Gajjar   

GROUP BY groups the result set into summary rows by provided columns. For example, consider below data which contains sales figures by region.

Group                Name                 SalesYTD
——————               ——————-              —————
North America        Northwest            123237.00
North America        Northwest             37534.00
North America        Northwest             48003.00
North America        Southwest            164232.00
North America        Southeast             39667.00
North America        Southeast            105810.00
Europe               France                74569.00
Europe               Germany               59456.00
Pacific              Australia             93403.00
Europe               United Kingdom        78327.00

This data can be summarized using a GROUP BY clause as below:

-- © 2011 – Vishal (http://SqlAndMe.com)
 
SELECT      [Group], [Name], SUM([SalesYTD]) AS 'Total Sales'
FROM        #TempTable
GROUP BY    [Group], [Name]
ORDER BY    [Group], [Name]

Result Set:

Group                Name                 Total Sales
——————               ——————               —————–
Europe               France                74569.00
Europe               Germany               59456.00
Europe               United Kingdom        78327.00
North America        Northwest            208774.00
North America        Southeast            145477.00
North America        Southwest            164232.00
Pacific              Australia             93403.00
 
(7 row(s) affected)

In the above result we can see that the data is summarized by [Group] and [Name].

WITH ROLLUP:

ROLLUP can be used to generate a subtotal rows and a grand total row for aggregate rows.

SELECT      [Group], [Name], SUM([SalesYTD]) AS 'Total Sales'
FROM        #TempTable
GROUP BY    [Group], [Name] WITH ROLLUP

Result Set:

Group                Name                 Total Sales
——————-              ——————-              ———————
Europe               France                74569.00
Europe               Germany               59456.00
Europe               United Kingdom        78327.00
Europe               NULL                 212352.00
North America        Northwest            208774.00
North America        Southeast            145477.00
North America        Southwest            164232.00
North America        NULL                 518483.00
Pacific              Australia             93403.00
Pacific              NULL                  93403.00
NULL                 NULL                 824238.00
 
(11 row(s) affected)

As we can see in the result set, 4 rows have been added, 3 for sub total of each [Group], and 1 row for grand total.

GROUPING():

GROUPING() function can be used to check whether the row is aggregated or not. It returns 1 for aggregated rows.

-- © 2011 – Vishal (http://SqlAndMe.com)
 
SELECT      [Group], [Name], SUM([SalesYTD])  AS 'Total Sales',
            GROUPING([Group]) AS 'Aggregated',
            GROUPING([Name]) AS 'Aggregated'
FROM        #TempTable
GROUP BY    [Group], [Name] WITH ROLLUP

Result Set:

Group                Name                 Total Sales   Aggregated    Aggregated
——————-              ——————–              ————-         ————          ————
Europe               France                74569.00     0             0
Europe               Germany               59456.00     0             0
Europe               United Kingdom        78327.00     0             0
Europe               NULL                 212352.00     0             1
North America        Northwest            208774.00     0             0
North America        Southeast            145477.00     0             0
North America        Southwest            164232.00     0             0
North America        NULL                 518483.00     0             1
Pacific              Australia             93403.00     0             0
Pacific              NULL                  93403.00     0             1
NULL                 NULL                 824238.00     1             1
 
(11 row(s) affected)

Finally, we can use GROUPING() function to identify and replace the 'NULL' in aggregated rows to something meaningful. for example, 'ALL'!!!!.

SELECT      CASE GROUPING([Group])
                  WHEN 1 THEN 'ALL'
                  ELSE [Group] END AS 'Group',
            CASE GROUPING([Name])
                  WHEN 1 THEN 'ALL'
                  ELSE [Name] END AS 'Name',
            SUM([SalesYTD])  AS 'Total Sales'
FROM        #TempTable
GROUP BY    [Group], [Name] WITH ROLLUP

Result Set:

Group                Name                 Total Sales
——————               ——————-              ——————–
Europe               France                74569.00
Europe               Germany               59456.00
Europe               United Kingdom        78327.00
Europe               ALL                  212352.00
North America        Northwest            208774.00
North America        Southeast            145477.00
North America        Southwest            164232.00
North America        ALL                  518483.00
Pacific              Australia             93403.00
Pacific              ALL                   93403.00
ALL                  ALL                  824238.00
 
(11 row(s) affected)

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]