Home   |  Articles   |  Resources   |  Humor   |  Feedback       

  Login   Register 

Ads Via DevMavens


SQL
Using ROLLUP to aggregate data in SQL

Posted by on Saturday, January 17, 2004

Using SQL's Aggregate Functions in particular WITH ROLLUP is an efficient, time effective way to generate column totals.

If  I was to compile a list of SQL’s most under used, unknown funtions – rollup and cube certainly top the list. Considering the hoops developers jump though to aggregate data in code I have no idea why these SQL statements are so underused. In this article we are going to look at with rollup what it is, what it does and how we can use it.

with rollup is an extension to the group by clause which can be used to generate aggregate groupings in a single query. It is used in conjunction with the aggregate function grouping and causes and additional column to be added to the record set which has a value of ‘1’. This allows us to differentiate the summary row from the data row where the value is 0. Lets look at the following example which you can run using the Northwind database.

SELECT
  CASE
      WHEN (Grouping(C.CustomerID)=1) THEN 'Report Total'
      ELSE C.CustomerID
  END AS CustomerID,

  CASE
      WHEN (Grouping(O.OrderID)=1) THEN 'Customer Totals'
      ELSE cast(O.OrderID as varchar(20))
  END AS OrderID, 

  CASE
      WHEN (Grouping(OD.ProductID)=1) THEN 'Order Totals'
      ELSE cast(OD.ProductID as varchar(20))
  END AS ProductID, 

  Sum(UnitPrice) as UnitPrice, 
  Sum(Quantity) as Quantity 

FROM [Order Details] OD
INNER JOIN Orders O
 on O.OrderID = OD.OrderID
Inner Join Customers C
 on C.CustomerID = O.CustomerID
GROUP BY C.CustomerID, O.OrderID,OD.ProductID with rollup

 Lets look at the results we get when we run this query

If we scroll down to the end of the report, we will also see the Total Unit Price and Total Quantity for the Report.
As we can see what the ROLLUP clause does is let SQL know that it needs to display both the detail and the aggregate total rows for each item in the GROUP by clause.

 

 

by Poonam Lall


Copyright 2004 OdeToCode.com 


The Blogs
Subscribe to the OdeToCode blogs for the latest news, downloads, new articles, and quirky commentary.
New Articles
Databinding in Silverlight
This article will cover data binding features in Silverlight, including binding expressions, validation, converters, and binding modes.

The Standard LINQ Operators
This article will cover the standard LINQ operators provided by LINQ for filtering, grouping, joining, converting, projecting, and more.

C# 3.0 and LINQ
C# 3.0 introduced a number of new features for LINQ. In this article we'll examine the new features like extension methods, lambda expressions, anonymous types, and more.

Most Popular Articles
Table Variables In T-SQL
Table variables allow you to store a resultset in SQL Server without the overhead of declaring and cleaning up a temporary table. In this article, we will highlight the features and advantages of the table variable data type.

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
MasterPages are a great addition to the ASP.NET 2.0 feature set, but are not without their quirks. This article will highlight the common problems developers face with master pages, and provide tips and tricks to use master pages to their fullest potential.

AppSettings In web.config
In this article we will review a couple of pratices to keep your runtime configuration information flexible.

Contribute Code
Privacy
Consultancy