solving sql server problems for millions of dbas and developers since 2006


Identify and resolve SQL Server problems BEFORE they happen with SQL diagnostic manager

SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Whitepapers SQL Server Tools SQL Server Webcasts SQL Server Questions and Answers SQL Server Questions and Answers






SQL Product Highlight

Idera - SQL safe backup

Idera's SQL safe provides a high-performance backup and recovery solution for Microsoft SQL Server. SQL safe saves money by reducing database backup time by up to 50% over native backups and reducing backup disk space requirements by up to 95%. SQL safe also enables complete "hands-free" automated backup of your entire SQL Server infrastructure and ensures compliance with your organization's backup and recovery policies. From implementations with tens of SQL servers to enterprises with hundreds of servers spread around the globe, SQL safe is the only SQL Server backup and recovery solution th

Learn more!




SQL Server 2008 Group By Sets

By: | Read Comments | Print

Chad is an Architect, Administrator and Developer with technologies such as SQL Server, .NET, and Windows Server.

Related Tips: More

With Sql 2008 (in the current CTP you have in your hand), thanks to a new extension to the group by clause referred to as 'grouping sets', you now have the ability to use sets of grouping columns in your group by clauses, allowing you to define basically multiple groupings in the same single query - i.e., instead of providing a single group by column-set, you can define multiple 'sets' of grouping columns, and have the resultset include the concatenated results for each set (think of this as effectively the UNION ALL of multiple identical select statements that are grouped by different column sets).

Some sample code showing some simple usage is here below, enjoy!

  use tempdb;
  go
  if object_id('dbo.tblGroupTest') > 0
   drop table dbo.tblGroupTest;
  create table dbo.tblGroupTest (id int, year smallint, quarter smallint, month smallint, amt bigint);
  go
  -- Fill up some sample yearly, quarterly, monthtly data for 2000 - 2007...
  declare @i int
  set @i = 2000
  while @i <= 2007 begin
   insert dbo.tblGroupTest (id, year, quarter, month, amt)
   select row_number() over (order by a.object_id),
     @i,
     ntile(4) over (order by a.object_id),
     ntile(12) over (order by a.object_id),
     a.object_id
   from (
     select object_id from sys.columns
     union all
     select object_id from sys.columns
     ) a;
 
   set @i = @i+1;
  end
  go
 
  -- Report
  select year,
    case when grouping(quarter) = 1 then '-- TOTAL --' else cast(quarter as varchar(5)) end as quarter,
    case when grouping(month) = 1 then '-- TOTAL --' else cast(month as varchar(5)) end as month,
    sum(amt) as sumAmt, avg(amt) as avgAmt
  from dbo.tblGroupTest
  group by grouping sets (
   (year, quarter, month),
   (year, quarter),
   (year)
  )
  order by year, isnull(quarter,10), isnull(month,15);


Chad Boyd ~~~ This posting is provided "AS IS" with no warranties, and confers no rights. Use of any included script samples are subject to the terms specified at http://www.mssqltips.com/disclaimer.asp and http://www.mssqltips.com/copyright.asp.

 



Related Tips: More | Become a paid author


Last Update: 10/15/2007

Share: Share 






Comments and Feedback:


Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"SQL diagnostic manager delivers response in minutes, not hours!"

It takes just 5 minutes to connect your SQL Databases to source control. Got 5 minutes? Get started now.

Make the most out of SQL Server - Guaranteed Results - Innovative SQL Server DBAs

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

Optimizing SQL Server performance can be a daunting task. Or is it?


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com