Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions...
-
Upload
nelson-hardy -
Category
Documents
-
view
221 -
download
3
description
Transcript of Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions...
Module 4: Grouping and Summarizing Data
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
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
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
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
GROUP BY Fundamentals
Using the GROUP BY Clause Using the GROUP BY Clause with the HAVING Clause
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
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
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
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
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
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
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
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
Lab A: Grouping and Summarizing Data
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