Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How the grouping set works in Sql2008 Expand / Collapse
Author
Message
Posted Saturday, December 7, 2013 2:51 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 5:05 AM
Points: 119, Visits: 221
Can anyone tell me how exactly grouping set works in sql 2008.how it is different from group by clause.
Post #1520804
Posted Saturday, December 7, 2013 1:25 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:04 PM
Points: 2,933, Visits: 2,953
You can use GROUP BY additionally with GROUPING SETS.
If you've been useing GROUP BY for a number of columns, now imagine those columns as sets, and everything is the same. When you use sets to make the groupation you have to add the GROUPING SETS as reserved words.

For example, the following select using GROUPING SETS

SELECT Region, Department, avg(sal) AverageSalary 
from tblEmployee
Group BY
GROUPING SETS
(
(Region, Department)
/*
(Region),
(Department) ,
()
*/
)

is equal to the following with only GROUP BY

SELECT Region, Department, avg(sal) AverageSalary 
from tblEmployee
Group BY
Region, Department


Regards,
IgorMi




Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1520838
Posted Sunday, December 8, 2013 12:34 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 5:05 AM
Points: 119, Visits: 221
Why should one use grouping sets when group by is available .how does it help?
Post #1520880
Posted Sunday, December 8, 2013 12:50 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:04 PM
Points: 2,933, Visits: 2,953
sej2008 (12/8/2013)
Why should one use grouping sets when group by is available .how does it help?


GROUPING SETS is less frequently used, but sometimes it can be useful if you're aware of its usefulness.

For example, the result set of this query will be first grouped by the grouping set (Region, Department), and then by City.

SELECT Region, Department, avg(sal) AverageSalary 
from tblEmployee
Group BY
GROUPING SETS
(
(Region, Department),
(City)
)

In this case the above select will not return the same ordered result set as

SELECT Region, Department, avg(sal) AverageSalary 
from tblEmployee
Group BY
Region, Department, City

Having got the example from the previous post, now you should have understood grouping sets...

Regards,
IgorMi




Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1520882
Posted Sunday, December 8, 2013 1:47 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:04 PM
Points: 2,933, Visits: 2,953
You can see some results and experiment with queries given in this link http://technet.microsoft.com/en-us/library/bb522495(v=sql.105).aspx



Igor Micev,
SQL Server developer at Seavus
www.seavus.com
Post #1520889
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse