Module 3: Grouping and Summarizing Data. Summarizing Data by Using Aggregate Functions Summarizing Grouped Data Ranking Grouped Data Creating Crosstab.

Slides:



Advertisements
Similar presentations
Module 4: Joining Data from Multiple Tables. Querying Multiple Tables by Using Joins Applying Joins for Typical Reporting Needs Combining and Limiting.
Advertisements

BY LECTURER/ AISHA DAWOOD DW Lab # 4 Overview of Extraction, Transformation, and Loading.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
5.1Database System Concepts - 6 th Edition Chapter 5: Advanced SQL Advanced Aggregation Features OLAP.
Chapter 11 Group Functions
Chapter 11 Group Functions (up to p.402)
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates.
Chap8: Trends in DBMS 8.1 Database support for Field Entities 8.2 Content-based retrieval 8.3 Introduction to spatial data warehouses 8.4 Summary.
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Integrating Business Intelligence with the Enterprise Peter Thanisch.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Version 1.0. MCAD MCSD MCPD Enterprise SQL MCTS MCT Software/Web Development Consultant Cryptography/Digital Signature Consultant SQL Server 2005/2008R2/2012.
Module 8 Improving Performance through Nonclustered Indexes.
1 Chapter 4: Creating Simple Queries 4.1 Introduction to Querying Data 4.2 Filtering and Sorting Data 4.3 Creating New Columns with an Expression 4.4 Grouping.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Module 9 Designing and Implementing Stored Procedures.
Dashboard Creation Mapping SQL queries to Google Chart Images.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
04 | Grouping and Aggregating Data Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
IMS 4212: Intro to SQL 1 Dr. Lawrence West, Management Dept., University of Central Florida Introduction to SQL—Topics Introduction to.
Module 18 Querying XML Data in SQL Server® 2008 R2.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
In this session, you will learn to: Use functions to customize the result set Summarize and group data Objectives.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Module 11 Authorizing Users to Access Resources. Module Overview Authorizing User Access to Objects Authorizing Users to Execute Code Configuring Permissions.
(SQL - Structured Query Language)
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
Parametre og variable i T-SQL 1.Parametre (input) 2.Parametre (output) 3.Variable.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Module 2: Authoring Basic Reports. Overview Creating a Basic Table Report Formatting Report Pages Calculating Values.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Module 2: Querying and Filtering Data. Using the SELECT Statement Filtering Data Working with NULL Values Formatting Result Sets Performance Considerations.
Module 6: Modifying Data in Tables. Inserting Data into Tables Deleting Data from Tables Updating Data in Tables Overview of Transactions.
Week 5 – Nov 4, 2015 Data Analysis. Class today Last week & Homework review SQL: Purpose and functions Data Cleaning Pivot Tables, Power Pivots and Power.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
Module 5: Working with Subqueries. Writing Basic Subqueries Writing Correlated Subqueries Comparing Subqueries with Joins and Temporary Tables Using Common.
2 Copyright © 2008, Oracle. All rights reserved. Building the Physical Layer of a Repository.
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Pete Harris | Microsoft Senior Content Publisher.
Module 4: Joining Data from Multiple Tables
In this session, you will learn to:
Relational Database Design
Writing Basic SQL SELECT Statements
02 | Advanced SELECT Statements
Database Systems Subqueries, Aggregation
Group Functions Lab 6.
Objectives Describe an overview of Transact-SQL programming
20761A 10: Using Subqueries Module 10   Using Subqueries.
05 | Using Functions and Aggregating Data
20761A 11: Using Set Operators Module 11   Using Set Operators.
Using Window Ranking, Offset, and Aggregate Functions
08 | Grouping Sets and Pivoting Data
20761B 12: Using Set Operators Module 12   Using Set Operators.
Writing SELECT Queries
02 | Querying Tables with SELECT
20761B 10: Using Subqueries Module 10   Using Subqueries.
Data warehouse Design Using Oracle
SQL – Entire Select.
Chapter 4 Summary Query.
Module 10: Implementing Managed Code in the Database
02 | Querying Tables with SELECT
Intermediate Query Structure and Development
Pivoting and Grouping Sets
Grouping and Aggregating Data
Presentation transcript:

Module 3: Grouping and Summarizing Data

Summarizing Data by Using Aggregate Functions Summarizing Grouped Data Ranking Grouped Data Creating Crosstab Queries

Lesson 1: Summarizing Data by Using Aggregate Functions Aggregate Functions Native to SQL Server Using Aggregate Functions with NULL Values CLR Integration, Assemblies Implementing Custom Aggregate Functions

Aggregate Functions Native to SQL Server Can be used in:  The select list of a SELECT statement  A COMPUTE or COMPUTE BY clause  A HAVING clause USE AdventureWorks SELECT AVG(VacationHours)AS 'AverageVacationHours', SUM (SickLeaveHours) AS 'TotalSickLeaveHours‘ FROM HumanResources.Employee WHERE Title LIKE 'Vice President%' USE AdventureWorks SELECT AVG(VacationHours)AS 'AverageVacationHours', SUM (SickLeaveHours) AS 'TotalSickLeaveHours‘ FROM HumanResources.Employee WHERE Title LIKE 'Vice President%' USE AdventureWorks SELECT COUNT(*) FROM Sales.SalesPerson WHERE SalesQuota > 25000; USE AdventureWorks SELECT COUNT(*) FROM Sales.SalesPerson WHERE SalesQuota > 25000; USE AdventureWorks SELECT MAX(TaxRate) FROM Sales.SalesTaxRate GROUP BY TaxType; USE AdventureWorks SELECT MAX(TaxRate) FROM Sales.SalesTaxRate GROUP BY TaxType;

Using Aggregate Functions With NULL Values USE AdventureWorks SELECT AVG(ISNULL(Weight,0)) AS ‘AvgWeight’ FROM Production.Product USE AdventureWorks SELECT AVG(ISNULL(Weight,0)) AS ‘AvgWeight’ FROM Production.Product The COUNT(*) function is an exception and returns the total number of records in a table Use the ISNULL function to correct this issue NULL values may produce unexpected or incorrect results Most aggregate functions ignore NULL values

CLR Integration, Assemblies Leverage the.NET 2.0 API Deploy custom developed CLR assemblies within the SQL Server Process Introduced in SQL Server 2005

Implementing Custom Aggregate Functions CREATE ASSEMBLY StringUtilities FROM ‘PathToAssembly\StringUtilities.dll' WITH PERMISSION_SET=SAFE; CREATE ASSEMBLY StringUtilities FROM ‘PathToAssembly\StringUtilities.dll' WITH PERMISSION_SET=SAFE; CREATE AGGREGATE nvarchar(4000)) RETURNS nvarchar(4000) EXTERNAL NAME [StringUtilities].[Concatenate] CREATE AGGREGATE nvarchar(4000)) RETURNS nvarchar(4000) EXTERNAL NAME [StringUtilities].[Concatenate] Can be created using any.NET language such as C# and VB Custom programs in an assembly that are imported into SQL Server® using the integrated CLR

Demonstration: Using Common Aggregate Functions This demonstration shows how to use some of the common aggregate functions in SQL Server The Human Resources (HR) department of Adventure Works requires information about employees. This would be an excellent situation to show the usage of several aggregate functions.

Lesson 2: Summarizing Grouped Data Using the GROUP BY clause Filtering Grouped Data by Using the HAVING Clause Building a Query for Summarizing Grouped Data – GROUP BY Examining How the ROLLUP and CUBE Operators Work Using the ROLLUP and CUBE Operators Using the COMPUTE and COMPUTE BY Clauses Building a Query for Summarizing Grouped Data - COMPUTE Using GROUPING SETS

Using the GROUP BY Clause SELECT SalesOrderID, SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail GROUP BY SalesOrderID ORDER BY SalesOrderID SELECT SalesOrderID, SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail GROUP BY SalesOrderID ORDER BY SalesOrderID Source Table Group 1 Group 2 Specifies the groups into which the output rows must be placed Calculates a summary value for aggregate functions SalesOrderIDSubTotal

Filtering Grouped Data by Using the HAVING Clause SELECT SalesOrderID, SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail GROUP BY SalesOrderID HAVING SUM(LineTotal) > ORDER BY SalesOrderID SELECT SalesOrderID, SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail GROUP BY SalesOrderID HAVING SUM(LineTotal) > ORDER BY SalesOrderID Specifies a search condition for a group Can be used only with the SELECT statement SalesOrderIDSubTotal

Building a Query for Summarizing Grouped Data – GROUP BY GROUP BY GROUP BY with HAVING clause SELECT A.City, COUNT(E. BusinessEntityID) EmployeeCount FROM HumanResources.Employee E INNER JOIN Person.Address A ON E.BusinessEntityID = A.AddressID GROUP BY A.City ORDER BY A.City; SELECT A.City, COUNT(E. BusinessEntityID) EmployeeCount FROM HumanResources.Employee E INNER JOIN Person.Address A ON E.BusinessEntityID = A.AddressID GROUP BY A.City ORDER BY A.City; SELECT DATEPART(yyyy,OrderDate) AS 'Year',SUM(TotalDue) AS 'Total Order Amount' FROM Sales.SalesOrderHeader GROUP BY DATEPART(yyyy,OrderDate) HAVING DATEPART(yyyy,OrderDate) >= '2003' ORDER BY DATEPART(yyyy,OrderDate) SELECT DATEPART(yyyy,OrderDate) AS 'Year',SUM(TotalDue) AS 'Total Order Amount' FROM Sales.SalesOrderHeader GROUP BY DATEPART(yyyy,OrderDate) HAVING DATEPART(yyyy,OrderDate) >= '2003' ORDER BY DATEPART(yyyy,OrderDate) CityEmployeeCount Bellevue35 Berlin1 Bordeaux1 Bothell 22 Calgary1 Cambridge2... YearTotal Order Amount

Examining How the ROLLUP and CUBE Operators Work SELECT a, b, c, SUM ( ) FROM T GROUP BY CUBE (a,b,c) SELECT a, b, c, SUM ( ) FROM T GROUP BY CUBE (a,b,c) SELECT a, b, c, SUM ( ) FROM T GROUP BY ROLLUP (a,b,c) SELECT a, b, c, SUM ( ) FROM T GROUP BY ROLLUP (a,b,c) ROLLUP and CUBE generate summary information in a query ROLLUP generates a result set showing the aggregates for a hierarchy of values in selected columns CUBE generates a result set that shows the aggregates for all combination of values in selected columns

Using the ROLLUP and CUBE Operators SELECT ProductID,Shelf,SUM(Quantity) AS QtySum FROM Production.ProductInventory WHERE ProductID<6 GROUP BY ROLLUP(ProductID,Shelf) SELECT ProductID,Shelf,SUM(Quantity) AS QtySum FROM Production.ProductInventory WHERE ProductID<6 GROUP BY ROLLUP(ProductID,Shelf) SELECT ProductID,Shelf,SUM(Quantity) AS QtySum FROM Production.ProductInventory WHERE ProductID<6 GROUP BY CUBE(ProductID, Shelf) SELECT ProductID,Shelf,SUM(Quantity) AS QtySum FROM Production.ProductInventory WHERE ProductID<6 GROUP BY CUBE(ProductID, Shelf) ProductIDShelfQtySum 1A761 1B324 1NULL1085 2A 791 2B 318 2NULL1109 3A 909 3B 443 3NULL1352 4A 900 ProductIDShelfQtySum 1A761 2A791 3A909 4A900 NULLA3361 1B324 2B318 3B443 4B442 NULLB1507

Demonstration: How to Use the ROLLUP and CUBE Operators This demonstration shows how to use the ROLLUP and CUBE operator extensions of the GROUP BY SELECT clause. You have been asked to summarize some information in the AdventureWorks database. This would be an excellent usage of these new operators.

Examining the COMPUTE and COMPUTE BY Clauses COMPUTE generates additional summary rows in a non- relational format COMPUTE BY generates control-breaks and subtotals in the result set Both COMPUTE and COMPUTE BY can be used in the same query

Building a Query For Summarizing Grouped Data - COMPUTE COMPUTE COMPUTE BY SELECT SalesOrderID, UnitPrice, UnitPriceDiscount FROM Sales.SalesOrderDetail ORDER BY SalesOrderID COMPUTE SUM(UnitPrice), SUM(UnitPriceDiscount) SELECT SalesOrderID, UnitPrice, UnitPriceDiscount FROM Sales.SalesOrderDetail ORDER BY SalesOrderID COMPUTE SUM(UnitPrice), SUM(UnitPriceDiscount) SELECT SalesPersonID, CustomerID, OrderDate, SubTotal, TotalDue FROM Sales.SalesOrderHeader ORDER BY SalesPersonID, OrderDate COMPUTE SUM(SubTotal), SUM(TotalDue) BY SalesPersonID SELECT SalesPersonID, CustomerID, OrderDate, SubTotal, TotalDue FROM Sales.SalesOrderHeader ORDER BY SalesPersonID, OrderDate COMPUTE SUM(SubTotal), SUM(TotalDue) BY SalesPersonID SalesOrderIDUnitPriceUnitPriceDiscount NULL1085 sumSum

Using GROUPING SETS New GROUP BY operator that aggregates several groupings in one query Eliminates multiple GROUP BY queries Supports additional options as ability to use with ROLLUP and CUBE operators SELECT T.[Group] AS 'Region', T.CountryRegionCode AS 'Country',S.Name AS 'Store', H.SalesPersonID,SUM(TotalDue) AS 'Total Sales' FROM Sales.Customer C INNER JOIN Sales.Store S ON C.StoreID = S.BusinessEntityID INNER JOIN Sales.SalesTerritory T ON C.TerritoryID = T.TerritoryID INNER JOIN Sales.SalesOrderHeader H ON C.CustomerID = H.CustomerID WHERE T.[Group] = 'Europe' AND T.CountryRegionCode IN('DE', 'FR') AND SUBSTRING(S.Name,1,4)IN('Vers', 'Spa ') GROUP BY GROUPING SETS (T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID) ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID; SELECT T.[Group] AS 'Region', T.CountryRegionCode AS 'Country',S.Name AS 'Store', H.SalesPersonID,SUM(TotalDue) AS 'Total Sales' FROM Sales.Customer C INNER JOIN Sales.Store S ON C.StoreID = S.BusinessEntityID INNER JOIN Sales.SalesTerritory T ON C.TerritoryID = T.TerritoryID INNER JOIN Sales.SalesOrderHeader H ON C.CustomerID = H.CustomerID WHERE T.[Group] = 'Europe' AND T.CountryRegionCode IN('DE', 'FR') AND SUBSTRING(S.Name,1,4)IN('Vers', 'Spa ') GROUP BY GROUPING SETS (T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID) ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID; RegionCountryStore NULL Spa and Exercise Outfitters NULL Versatile Sporting Good Company NULLDENULL FRNULL EuropeNULL

Lesson 3: Ranking Grouped Data What Is Ranking? Ranking Data by Using RANK Ranking Data by Using DENSE_RANK Ranking Data by Using ROW_NUMBER Ranking Data by Using NTILE Categorizing the Ranking Functions Based On Their Functionality

What s Ranking? Ranking computes a value for each row in user-specified set of rows. Ranking functions include: Can be used for: RANK DENSE_RANK NTILE ROW_NUMBER Arbitrary row numbering Product Popularity Best Customers

Ranking Data by Using RANK SELECT P.Name Product, P.ListPrice, PSC.Name Category, RANK() OVER(PARTITION BY PSC.Name ORDER BY P.ListPrice DESC) AS PriceRank FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID SELECT P.Name Product, P.ListPrice, PSC.Name Category, RANK() OVER(PARTITION BY PSC.Name ORDER BY P.ListPrice DESC) AS PriceRank FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID Product ListPrice CategoryPriceRank Men's Bib-Shorts, S89.99Bib-Shorts 1 Men's Bib-Shorts, M Bib-Shorts 1 Men's Bib-Shorts, L Bib-Shorts 1 Hitch Rack - 4-Bike Bike Racks 1 All-Purpose Bike Stand Bike Stands 1 Mountain Bottle Cage 9.99 Bottles and Cages 1 Road Bottle Cage 8.99 Bottles and Cages 2 Product ListPrice CategoryPriceRank Men's Bib-Shorts, S89.99Bib-Shorts 1 Men's Bib-Shorts, M Bib-Shorts 1 Men's Bib-Shorts, L Bib-Shorts 1 Hitch Rack - 4-Bike Bike Racks 1 All-Purpose Bike Stand Bike Stands 1 Mountain Bottle Cage 9.99 Bottles and Cages 1 Road Bottle Cage 8.99 Bottles and Cages 2

Ranking Data by Using DENSE_RANK SELECT P.Name Product, P.ListPrice, PSC.Name Category, DENSE_RANK() OVER(PARTITION BY PSC.Name ORDER BY P.ListPrice DESC) AS PriceRank FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID SELECT P.Name Product, P.ListPrice, PSC.Name Category, DENSE_RANK() OVER(PARTITION BY PSC.Name ORDER BY P.ListPrice DESC) AS PriceRank FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID ProductListPriceCategoryPriceRank HL Mountain Handlebars120.27Handlebars1 HL Road Handlebars120.27Handlebars1 HL Touring Handlebars91.57Handlebars2 ML Mountain Handlebars61.92Handlebars3 HL Headset124.73Headsets1 ML Headset102.29Headsets2 LL Headset34.20Headsets3 ProductListPriceCategoryPriceRank HL Mountain Handlebars120.27Handlebars1 HL Road Handlebars120.27Handlebars1 HL Touring Handlebars91.57Handlebars2 ML Mountain Handlebars61.92Handlebars3 HL Headset124.73Headsets1 ML Headset102.29Headsets2 LL Headset34.20Headsets3

Ranking Data by Using ROW_NUMBER SELECT ROW_NUMBER() OVER(PARTITION BY PC.Name ORDER BY ListPrice) AS Row, PC.Name Category, P.Name Product, P.ListPrice FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID JOIN Production.ProductCategory PC ON PSC.ProductCategoryID = PC.ProductCategoryID SELECT ROW_NUMBER() OVER(PARTITION BY PC.Name ORDER BY ListPrice) AS Row, PC.Name Category, P.Name Product, P.ListPrice FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID JOIN Production.ProductCategory PC ON PSC.ProductCategoryID = PC.ProductCategoryID RowCategoryProductListPrice 1AccessoriesPatch Kit/8 Patches AccessoriesRoad Tire Tube BikesRoad-750 Black, BikesRoad-750 Black, RowCategoryProductListPrice 1AccessoriesPatch Kit/8 Patches AccessoriesRoad Tire Tube BikesRoad-750 Black, BikesRoad-750 Black,

Ranking Data by Using NTILE SELECT NTILE(3) OVER(PARTITION BY PC.Name ORDER BY ListPrice) AS PriceBand, PC.Name Category, P.Name Product, P.ListPrice FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID JOIN Production.ProductCategory PC ON PSC.ProductCategoryID = PC.ProductCategoryID SELECT NTILE(3) OVER(PARTITION BY PC.Name ORDER BY ListPrice) AS PriceBand, PC.Name Category, P.Name Product, P.ListPrice FROM Production.Product P JOIN Production.ProductSubCategory PSC ON P.ProductSubCategoryID = PSC.ProductSubCategoryID JOIN Production.ProductCategory PC ON PSC.ProductCategoryID = PC.ProductCategoryID PriceBandCategoryProductListPrice 1AccessoriesPatch Kit/8 Patches2.29 1AccessoriesRoad Tire Tube3.99 2AccessoriesLL Road Tire AccessoriesML Road Tire AccessoriesSport-100 Helmet, Blue AccessoriesHL Mountain Tire BikesRoad-750 Black, BikesRoad-650 Red, BikesRoad-650 Red, PriceBandCategoryProductListPrice 1AccessoriesPatch Kit/8 Patches2.29 1AccessoriesRoad Tire Tube3.99 2AccessoriesLL Road Tire AccessoriesML Road Tire AccessoriesSport-100 Helmet, Blue AccessoriesHL Mountain Tire BikesRoad-750 Black, BikesRoad-650 Red, BikesRoad-650 Red,

Categorizing the Ranking Functions Based On Their Functionality FeatureRANKDENSE_RANKNTILEROW_NUMBER Rows equally distributed among groups Rows of equal rank are assigned the same value Sequential numbering of rows within a partition

Lesson 4: Creating Crosstab Queries How the PIVOT and UNPIVOT Operators Work Using the PIVOT Operator Using the UNPIVOT Operator Grouping and Summarization Features New to SQL Server 2008

How the PIVOT and UNPIVOT Operators Work CustBikeChain Mike82 Lisa37 PIVOT – converts values to columns CustProdQty MikeBike8 MikeChain2 LisaBike3 LisaChain7 CustBikeChain Mike82 Lisa37 CustProdQty MikeBike3 MikeChain2 MikeBike5 LisaBike3 LisaChain3 LisaChain4 UNPIVOT – converts columns to values

Using the PIVOT Operator CustBikeChain Mike82 Lisa37 PIVOT – converts values to columns SELECT * FROM Sales.Order PIVOT (SUM(Qty) FOR Prod IN ([Bike],[Chain])) PVT SELECT * FROM Sales.Order PIVOT (SUM(Qty) FOR Prod IN ([Bike],[Chain])) PVT CustProdQty MikeBike3 MikeChain2 MikeBike5 LisaBike3 LisaChain3 LisaChain4

Using the UNPIVOT Operator CustProdQty MikeBike8 MikeChain2 LisaBike3 LisaChain7 SELECT Cust, Prod, Qty FROM Sales.PivotedOrder UNPIVOT (Qty FOR Prod IN ([Bike],[Chain])) UnPVT SELECT Cust, Prod, Qty FROM Sales.PivotedOrder UNPIVOT (Qty FOR Prod IN ([Bike],[Chain])) UnPVT CustBikeChain Mike82 Lisa37 UNPIVOT – converts columns to values

Grouping and Summarization Features New to SQL Server 2008 CUBE Operator ROLLUP Operator GROUPING SETS Operator GROUPING_ID Function

Lab: Grouping and Summarizing DataPage 127 Exercise 1: Summarizing Data by Using Aggregate Functions Exercise 2: Summarizing Grouped Data Exercise 3: Ranking Grouped Data Exercise 4: Creating Crosstab Queries Logon information Virtual machineNY-SQL-01 User nameAdministrator Password Pa$$w0rd Estimated time: 60 minutes

Lab Scenario You are the database administrator of Adventure Works. The HR department wants you to prepare a report on the number of vacation hours of the vice presidents of the company, and another report on the number of employees with incomplete addresses. The warehouse manager requires three reports on the average number of hours taken to manufacture a product, the order quantity and the price list of each product, and the safety stock level for red, blue, and black helmets. Besides these requests, the marketing manager wants you to prepare a report on the year-to-date sales of salespersons and rank their names based on their performance.

Lab Review When you execute queries with aggregate functions you may see a warning message “Null value is eliminated by an aggregate or other SET operation”. Why do you get this? When executing your query you receive the error “Column xyz is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause”. What did you forget to do? When ranking the salespersons into four categories based on their year to date sales, were the amount of results in each category even? Why or why not?

Module Review and Takeaways Review Questions