Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions...

16
Module 4: Grouping and Summarizing Data

description

Listing the TOP n Values Lists Only the First n Rows of a Result Set Specifies the Range of Values in the ORDER BY Clause Returns Ties if WITH TIES Is Used USE northwind SELECT TOP 5 orderid, productid, quantity FROM [order details] ORDER BY quantity DESC GO USE northwind SELECT TOP 5 orderid, productid, quantity FROM [order details] ORDER BY quantity DESC GO USE northwind SELECT TOP 5 WITH TIES orderid, productid, quantity FROM [order details] ORDER BY quantity DESC GO USE northwind SELECT TOP 5 WITH TIES orderid, productid, quantity FROM [order details] ORDER BY quantity DESC GO Example 1 Example 2

Transcript of Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions...

Page 1: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Module 4: Grouping and Summarizing Data

Page 2: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Overview

Listing the TOP n Values

Using Aggregate Functions

GROUP BY Fundamentals

Generating Aggregate Values Within Result Sets

Using the COMPUTE and COMPUTE BY Clauses

Page 3: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Listing the TOP n Values

Lists Only the First n Rows of a Result Set Specifies the Range of Values in the ORDER BY Clause Returns Ties if WITH TIES Is Used

USE northwindSELECT TOP 5 orderid, productid, quantity FROM [order details] ORDER BY quantity DESCGO

USE northwindSELECT TOP 5 WITH TIES orderid, productid, quantity FROM [order details] ORDER BY quantity DESCGO

Example 1

Example 2

Page 4: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Using Aggregate Functions

Aggregate functionAggregate function DescriptionDescriptionAVG Average of values in a numeric expression

COUNT Number of values in an expression

COUNT (*) Number of selected rows

MAX Highest value in the expression

MIN Lowest value in the expression

SUM Total values in a numeric expression

STDEV Statistical deviation of all values

STDEVP Statistical deviation for the population

VAR Statistical variance of all values

VARP Statistical variance of all values for the population

Page 5: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Using Aggregate Functions with Null Values

Most Aggregate Functions Ignore Null Values COUNT(*) Function Counts Rows with Null Values

USE northwindSELECT COUNT (*) FROM employeesGO

USE northwindSELECT COUNT(reportsto) FROM employeesGO

Example 1

Example 2

Page 6: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

GROUP BY Fundamentals

Using the GROUP BY Clause Using the GROUP BY Clause with the HAVING Clause

Page 7: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Using the GROUP BY ClauseUSE northwindSELECT productid, orderid ,quantity FROM orderhistGO

USE northwindSELECT productid ,SUM(quantity) AS total_quantity FROM orderhist GROUP BY productidGO

productidproductid total_quantitytotal_quantity1 15

2 35

3 45

productidproductid orderidorderid quantityquantity1 1 5

1 1 10

2 1 10

2 2 25

3 1 15

3 2 30

productidproductid total_quantitytotal_quantity2 35

Only rows thatsatisfy the WHERE clause are grouped

USE northwindSELECT productid ,SUM(quantity) AS total_quantity FROM orderhist WHERE productid = 2 GROUP BY productidGO

Page 8: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Using the GROUP BY Clause with the HAVING Clause

USE northwindSELECT productid, orderid ,quantity FROM orderhistGO

USE northwindSELECT productid, SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid HAVING SUM(quantity)>=30GO

productidproductid total_quantitytotal_quantity2 35

3 45

productidproductid orderidorderid quantityquantity1 1 5

1 1 10

2 1 10

2 2 25

3 1 15

3 2 30

Page 9: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Generating Aggregate Values Within Result Sets

Using the GROUP BY Clause with the ROLLUP Operator Using the GROUP BY Clause with the CUBE Operator Using the GROUPING Function

Page 10: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Description

Using the GROUP BY Clause with the ROLLUP OperatorUSE northwindSELECT productid, orderid, SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid, orderid WITH ROLLUP ORDER BY productid, orderidGOproductidproductid orderidorderid total_quantitytotal_quantity

NULL NULL 951 NULL 151 1 51 2 102 NULL 352 1 102 2 253 NULL 453 1 153 2 30

Grand total

Summarizes only rows for productid 1Detail value for productid 1, orderid 1Detail value for productid 1, orderid 2Summarizes only rows for productid 2Detail value for productid 2, orderid 1Summarizes only rows for productid 3Detail value for productid 3, orderid 1Detail value for productid 3, orderid 2

Page 11: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Using the GROUP BY Clause with the CUBE Operator

The CUBE operatorproduces two more summaryvalues than theROLLUP operator

USE northwindSELECT productid, orderid, SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid, orderid WITH CUBE ORDER BY productid, orderidGO

DescriptionGrand total

Summarizes all rows for orderid 1Summarizes all rows for orderid 2Summarizes only rows for productid 1Detail value for productid 1, orderid 1Detail value for productid 1, orderid 2Summarizes only rows for productid 2Detail value for productid 2, orderid 1Detail value for productid 2, orderid 2Summarizes only rows for productid 3Detail value for productid 3, orderid 1Detail value for productid 3, orderid 2

productidproductid orderidorderid total_quantitytotal_quantityNULL NULL 95NULL 1 30NULL 2 65

1 NULL 151 1 51 2 102 NULL 352 1 102 2 253 NULL 453 1 153 2 30

Page 12: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

1 represents summary values in the preceding column

0 represents detail values in the preceding column

953065155

10351025451530

Using the GROUPING FunctionSELECT productid, GROUPING (productid) ,orderid, GROUPING (orderid) ,SUM(quantity) AS total_quantity FROM orderhist GROUP BY productid, orderid WITH CUBE ORDER BY productid, orderidGO

productidproductidNULLNULLNULL

111222333

111000000000

orderidorderidNULL

12

NULL12

NULL12

NULL12

100100100100

total_quantitytotal_quantity

Page 13: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Using the COMPUTE and COMPUTE BY ClausesCOMPUTE BYCOMPUTE

USE northwindSELECT productid, orderid, quantity FROM orderhist ORDER BY productid, orderid COMPUTE SUM(quantity) BY productid COMPUTE SUM(quantity)GO

USE northwindSELECT productid, orderid ,quantity FROM orderhistORDER BY productid, orderidCOMPUTE SUM(quantity)GO

productidproductid orderidorderid quantityquantity1 1 51 2 102 1 102 2 253 1 153 2 30

sum 95

productidproductid orderidorderid quantityquantity1 1 51 2 10

sum 152 1 102 2 25

sum 353 1 153 2 30

sum 45sum 95

Page 14: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Avoid Using the COMPUTE or COMPUTE BY Clause

Index Frequently Aggregated Columns

Avoid Using Aggregate Functions with Null Values

Use the ORDER BY Clause to Guarantee a Sort Order

Use the ROLLUP Operator Instead of the CUBE Operator

Recommended Practices

Page 15: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Lab A: Grouping and Summarizing Data

Page 16: Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.

Review

Listing the TOP n Values

Using Aggregate Functions

GROUP BY Fundamentals

Generating Aggregate Values Within Result Sets

Using the COMPUTE and COMPUTE BY Clauses