See my SQL-only BLOG at http://blogs.mssqltips.com/blogs/chadboyd Katmai (Sql 2008) - Group By Sets

Katmai (Sql 2008) - Group By Sets

by chad 10/31/2007 11:28:00 AM

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);

Currently rated 5.0 by 3 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Related posts

Powered by BlogEngine.NET 1.1.3.8
Theme by Mads Kristensen

About the author, Chad


Chad is an Architect, Administrator, and Developer with technologies such as Sql Server (and all related technologies), Windows Server, and Windows Clustering. He currently works with Gratis Internet Inc., an online marketing firm based in Washington, DC (www.gratisinternet.com), and also spends a significant amount of time writing, talking, presenting and blogging about Sql Server in the local DC area and online at mssqltips.com and his personal site, chadhoc.net. In the past, Chad has worked with companies and organizations such as Microsoft Corporation and The American Red Cross, and provided consulting/support services at companies such as Bank of America, HP, Citigroup, Qualcomm, Scottrade, TJX, SunTrust, and Zurich Financial Services. For over 2 years with Microsoft Corporation Chad was responsible for providing onsite and remote support, guidance, and advice with SQL Server products to some of Microsoft’s foremost enterprise customers running the largest, most complex SQL Server installations and configurations in the world. This included all SQL Server products and versions, including SQL Server 7.0, 2000, and 2005, the SQL Server database engine, Reporting Services, SSIS/DTS, Notification Services, and Analysis Services on both 32 and 64 bit systems. Chad's primary responsibilities today include troubleshooting critical server situations, performance tuning and monitoring, disaster recovery planning and execution, architectural guidance for new Sql Server related deployments, and delivering deep technical workshops/presentations/proof-of-concept sessions covering a variety of technologies and functionality.

E-mail me Send mail

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Pages

    Recent posts

    Recent comments

      Authors

      Categories


        Archive

        Blogroll

        Download OPML file OPML

        Disclaimer

        The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

        © Copyright 2008

        Sign in