SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How the grouping set works in Sql2008


How the grouping set works in Sql2008

Author
Message
sej2008
sej2008
Old Hand
Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)

Group: General Forum Members
Points: 369 Visits: 394
Can anyone tell me how exactly grouping set works in sql 2008.how it is different from group by clause.
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5892 Visits: 5080
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
My blog: www.igormicev.com
sej2008
sej2008
Old Hand
Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)Old Hand (369 reputation)

Group: General Forum Members
Points: 369 Visits: 394
Why should one use grouping sets when group by is available .how does it help?
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5892 Visits: 5080
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
My blog: www.igormicev.com
Igor Micev
Igor Micev
SSCertifiable
SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)SSCertifiable (5.9K reputation)

Group: General Forum Members
Points: 5892 Visits: 5080
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
My blog: www.igormicev.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search