Readme_Grouping Sets Report Sample
11/05/2008 21:36:06
This sample works only with SQL Server 2005 and SQL Server 2008. It will not work with any version of SQL Server earlier than SQL Server 2005.
This sample illustrates how to combine the SQL Server 2008 GROUPING SETS feature of Transact-SQL with the new Tablix capabilities of SQL Server 2008 Reporting Services to display the output of a GROUPING SETS query in a pivot-table-style format.
The GROUPING SETS feature allows you to write a single query which groups by multiple different sets of grouping columns, with complete control over what columns to group by. This can make the query easier to write and allow it to run faster than in prior SQL Server releases. Of course, it is desirable to render the output of a query in an appealing report format, and SQL Server Reporting Services is the tool of choice for that.
For more information, see Using GROUP BY with ROLLUP, CUBE, and GROUPING SETS in SQL Server 2008 Books Online.
Scenario
The goal of this sample is to produce a pivot-table-style report that looks like this:The report shows the number of distinct products sold per country for each quarter. It also shows the total number of distinct products sold for all countries for each quarter, as well as the total number of distinct products per country for six quarters. The distinct counts are based on results from the following query.
{" USE AdventureWorksDW2008;GOSELECT D.CalendarYear, D.CalendarQuarter, T.SalesTerritoryCountry, COUNT(DISTINCT F.ProductKey) AS DistinctProductCountFROM dbo.FactResellerSales F INNER JOIN dbo.DimDate D ON F.OrderDateKey = D.DateKey INNER JOIN dbo.DimSalesTerritory T ON F.SalesTerritoryKey = T.SalesTerritoryKey WHERE D.CalendarYear IN ('2003', '2004') GROUP BY GROUPING SETS ( (D.CalendarYear, D.CalendarQuarter, T.SalesTerritoryCountry), (D.CalendarYear, D.CalendarQuarter), (T.SalesTerritoryCountry),());GO "} |
The approach used in this sample is best for non-additive aggregates like COUNT(DISTINCT…) , STDEV , and AVG, whose aggregate results do not support commutative and associative properties. For example, given the set (1, 1, 2, 2), COUNT ( DISTINCT (1, 1) ) + COUNT ( DISTINCT (2, 2) ) = 2, but COUNT ( DISTINCT (1, 2) ) + COUNT ( DISTINCT ( 1, 2 ) ) = 4.
If you are using a grouping query that uses an additive aggregate (e.g. COUNT , SUM , MIN , or MAX ), you can use a query similar to the one below, which uses COUNT without grouping sets, and use the Tablix control in SQL Server 2008 Reporting Services to produce row and column aggregates and an overall aggregate.
{" USE AdventureWorksDW2008;GOSELECT D.CalendarYear, D.CalendarQuarter, T.SalesTerritoryCountry, COUNT(F.SalesAmount) as SalesAmountFROM dbo.FactResellerSales F INNER JOIN dbo.DimDate D ON F.OrderDateKey = D.DateKey INNER JOIN dbo.DimSalesTerritory T ON F.SalesTerritoryKey = T.SalesTerritoryKeyWHERE D.CalendarYear IN ('2003', '2004')GROUP BY D.CalendarYear, D.CalendarQuarter, T.SalesTerritoryCountry;GO "} |
It is easiest to compute aggregates for (Year,Quarter), (Country) and () groupings inside Reporting Services in this situation, rather than modify the query to use GROUPING SETS.
Languages
Transact-SQLPrerequisites
Before you run this sample, install the following software.- SQL Server 2008 and the following components:
- Database Engine
- Reporting Services
- Business Intelligence Development Tools
- Visual Studio
- samples for SQL Server 2008. The default installation directory is C:\Program Files\Microsoft SQL Server\100\Samples\Engine\Query Processing\Grouping Sets Report.
- AdventureWorksDW2008 sample database for SQL Server 2008. You can obtain this from the SQL Server Samples site on Codeplex Web site. For example, if you are using the x64 architecture, you can download SQL2008.AdventureWorksDWBI_v2008.x64.msi to install AdventureWorksDW2008. For more information, see Considerations for Installing SQL Server Samples and Sample Databases in SQL Server 2008 Books Online.
Running the Sample
The following steps show how to run the Grouping Sets Matrix Sample.rdl script to see the report based on the AdventureWorksDW 2008 sample database.- From the Start menu, open My Computer and navigate to the installation directory, C:\Program Files\Microsoft SQL Server\100\Samples\Engine\Query Processing\Grouping Sets Report.
- Double-click "Grouping Sets Report Project.sln" to open the project in Visual Studio.
- Expand Shared Data Sources, double-click AdventureWorksDW1.rds and adjust the settings in the dialog box so that you can connect to your AdventureWorksDW 2008 database.
- In Solution Explorer, expand the Reports folder, and double-click Grouping Sets Matrix Sample.rdl.
- Click the preview tab to see the report. Note the following interesting things about the sample: A new column group and row group have been added outside the default groups. These are named STCountry2 and CY2.
Summary
This sample showed how to create a pivot table report over a GROUPING SETS query using SQL Server 2008 Reporting Services (SSRS). Use this approach for non-additive aggregates such as COUNT(DISTINCT…), AVG, and STDEV. For additive aggregates such as SUM and COUNT, compute the row, column, and grand totals in Reporting Services instead, and do not use GROUPING SETS in your Transact-SQL query.© 2008 Microsoft Corporation. All rights reserved.