Wednesday 12 June 2019

SQL Server - GROUP BY

Summarizing and Aggregating Data
Being able to roll up and summarize data is important in many applications, particularly in financial and accounting. Though being able to aggregate and summarize data is also important in healthcare, telecommunication, marketing, and just about any other business area where one deals with numbers. The popularity of data warehousing and business intelligence in the marketplace is testimony to this.

Knowing how to write queries that can aggregate and summarize data is important if you are writing reports or engaged in many BI (Business Intelligence) types of activities.

On this page I will show you how to use GROUP BY to roll up data. We will be using the following aggregate functions.

AVG
SUM
COUNT
MIN
MAX
GROUP BY on a Single Column
In this example we are going to roll-up and get the total sales for 2007 broken down by category. When we are rolling up data there are two important things we require up in our SELECT. They are:

The column(s) we want to group the data on
The data columns we want to aggregate - Using SUM
It is also important to remember that your grouping column(s) must also be declared down in the GROUP BY clause. The GROUP BY clause comes after the WHERE clause.

It is also important to understand that any other non grouping columns up in the SELECT must have and aggregate function applied to them or you will recieve a syntax error.

USE AdventureWorksDW2008R2
GO

SELECT     
   DimProductCategory.EnglishProductCategoryName AS "Category"
  ,SUM(FactInternetSales.SalesAmount) AS "Total Sales"
  ,'$' + Convert(varchar,Convert(money,SUM(FactInternetSales.SalesAmount)),1) 
   AS "Formatted Total Sales"
FROM  DimProduct 
INNER JOIN DimProductSubcategory ON 
 DimProduct.ProductSubcategoryKey = DimProductSubcategory.ProductSubcategoryKey 
INNER JOIN DimProductCategory ON 
 DimProductSubcategory.ProductCategoryKey = DimProductCategory.ProductCategoryKey 
INNER JOIN FactInternetSales ON 
 DimProduct.ProductKey = FactInternetSales.ProductKey
WHERE OrderDateKey >= 20070101 AND OrderDateKey < 20080000
GROUP BY DimProductCategory.EnglishProductCategoryName
ORDER BY DimProductCategory.EnglishProductCategoryName   


groupby1

Please note that I added a column so you caould see how to format our sales amount as it would be typically displayed. However, this is something you typically are not going to have to do as all report writers will allows you do do this type of formatting in the report itself. Also, if you are passing the results to a host language program and use this technique you are sending it the value in a format that a) has been rounded and as such are losing precision, b) you are sending it as a character string which likely is not the desired format!

The SUM aggregate function is the most commonly used of the aggregate functions.

Multiple Aggregate Functions up in the Select - Adding AVG
We can add multiple aggregate functions up in the SELECT. For example

SELECT     
   DimProductCategory.EnglishProductCategoryName AS "Category"
  ,SUM(FactInternetSales.SalesAmount) AS "Total Sales"
  ,SUM(FactInternetSales.OrderQuantity) AS "Tot# Items Sold"
  ,AVG(FactInternetSales.SalesAmount) AS "AVG Sale"


avgsales 

You will note that I have added another summary column (OrderQuantity) to get the total number of items sold. I have also added the AVG aggregate function to the SalesAmount column. If you get out your calculator and multiply AVG(SalesAmount) by OrderQuantity you would expect to get the value found in SUM(SalesAmount). You will get close but not the exact amount due to rounding errors.



COUNT
You should be familiar with using COUNT(*) to determine the number of rows in a table. The sole purpose of the count function is to return the number of rows based upon your selection and/or group by and/or having critieria. Let's look at a few examples to understand this a little better.

If we extend the last example

   DimProductCategory.EnglishProductCategoryName AS "Category"
  ,SUM(FactInternetSales.SalesAmount) AS "Total Sales"
  ,SUM(FactInternetSales.OrderQuantity) AS "Tot# Items Sold"
  ,COUNT(FactInternetSales.OrderQuantity) AS "Tot# Rows1" 
  ,COUNT(FactInternetSales.SalesAmount) AS "Tot# Rows2"   
  ,COUNT(*) AS "Tot# Rows3" 
  ,AVG(FactInternetSales.SalesAmount) AS "AVG Sale"


count

If you notice the count is the same no matter which column we apply the function to. This is because count returns the number of rows in context to the selection criteria and grouping.

In this instance the return value of count is the SAME as the SUM(OrderQuantity). This is merely a coincidence as the their is no quantity greater than 1 in the recordset! It is always important to understand the data in the database or one can jump to incorrect conclusions when working with SQL.



orderqty 

So we can drive this all home lets look at an example that has a smaller dataset. Below is our full dataset.



simpletable 

Now lets look at a query equivalent to the one above.



simpletablegroupby 

You can clearly see that the SUM() function is adding up the values in the num1 column, whereas the COUNT() function is returning a count of the number of rows.

MIN/MAX
The MIN and MAX functions will simply return the smallest and the largest value for that column.

   DimProductCategory.EnglishProductCategoryName AS "Category"
  ,SUM(FactInternetSales.SalesAmount) AS "Total Sales"
  ,MIN(FactInternetSales.SalesAmount) AS "Lowest Sale Amount"
  ,MAX(FactInternetSales.SalesAmount) AS "Highest Sale Amount"  
  ,AVG(FactInternetSales.SalesAmount) AS "AVG Sales"


min_max 

Multiple GROUP BY Levels
The ability to provide an atomic recordset to BI software has made the task of rolling up and summarizing data easier... at least for the person doing this type of work. Tools like PowerPivot can take a recordset from either a dimensional model of typical OLTP database and allow on to quickly slice and dice it into many different pivot views without having to write these type of queries.

None the less, it is still good to know how to write SQL queries than rollup and aggregate data. In the following example we will rollup and get total sales by product.

USE AdventureWorksDW2008R2
GO

SELECT     
 DimProductCategory.EnglishProductCategoryName AS "Category",
 DimProductSubcategory.EnglishProductSubcategoryName AS "Subcategory",
 DimProduct.EnglishProductName AS "Product",
 SUM(FactInternetSales.OrderQuantity) AS "Tot Qty", 
  SUM(FactInternetSales.SalesAmount) AS "Tot Sales"
FROM    DimProduct 
INNER JOIN DimProductSubcategory ON 
DimProduct.ProductSubcategoryKey = DimProductSubcategory.ProductSubcategoryKey 
INNER JOIN DimProductCategory ON 
DimProductSubcategory.ProductCategoryKey = DimProductCategory.ProductCategoryKey 
INNER JOIN FactInternetSales ON 
DimProduct.ProductKey = FactInternetSales.ProductKey
WHERE OrderDateKey >= 20070101 AND OrderDateKey < 20080000
GROUP BY DimProductCategory.EnglishProductCategoryName, 
         DimProductSubcategory.EnglishProductSubcategoryName, 
         DimProduct.EnglishProductName   
ORDER BY DimProductCategory.EnglishProductCategoryName ASC,
         DimProductSubcategory.EnglishProductSubcategoryName ASC,
         DimProduct.EnglishProductName ASC


totsalesbycat 

We can define multiple group by levels. It is the last group by level that are data will be summarized against. It is possible to build a view against this query and them we would be able to take the existing recordset and roll it up to also report total sales by Subcategory and then by Category.

The more atomic out resultant recordset the great the ability we have to drill into it.

No comments:

Post a Comment