sql - 2071b 04

Upload: jose-alberto

Post on 31-May-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 SQL - 2071B 04

    1/16

    Module 4: Grouping andSummarizing Data

  • 8/14/2019 SQL - 2071B 04

    2/16

    Overview

    Listing the TOP n Values

    Using Aggregate Functions

    GROUP BY Fundamentals

    Generating Aggregate Values WithinResult Sets

    Using the COMPUTE and COMPUTE BYClauses

  • 8/14/2019 SQL - 2071B 04

    3/16

    Listing the TOP n Values

    Lists Only the First n Rows of a ResultSet

    Specifies the Range of Values in theORDER 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 WITH TIES orderid, productid, quantity

    FROM [order details]

    ORDER BY quantity DESC

    GO

    Example 1

    Example 2

  • 8/14/2019 SQL - 2071B 04

    4/16

    Using Aggregate Functions

    Aggregate functionAggregate function DescriptionDescription

    AVG 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

  • 8/14/2019 SQL - 2071B 04

    5/16

    Using Aggregate Functions with NullValues

    Most Aggregate Functions Ignore NullValues

    COUNT(*) Function Counts Rows with

    Null Values

    USE northwind

    SELECT COUNT (*)

    FROM employees

    GO

    USE northwind

    SELECT COUNT(reportsto)

    FROM employees

    GO

    Example 1

    Example 2

  • 8/14/2019 SQL - 2071B 04

    6/16

    GROUP BY Fundamentals

    Using the GROUP BY Clause

    Using the GROUP BY Clause with theHAVING Clause

  • 8/14/2019 SQL - 2071B 04

    7/16

    Using the GROUP BY Clause

    USE northwind

    SELECT productid, orderid

    ,quantityFROM orderhist

    GO

    USE northwind

    SELECT productid

    ,SUM(quantity) AS total_quantity

    FROM orderhist

    GROUP BY productid

    GO

    productidproductid total_quantitytotal_quantity

    1 15

    2 35

    3 45

    productidproductid orderidorderid quantityquantity

    1 1 5

    1 1 10

    2 1 10

    2 2 25

    3 1 15

    3 2 30

    productidproductid total_quantitytotal_quantity

    2 35

    Only rows thatsatisfy theWHERE clauseare grouped

    USE northwind

    SELECT productid

    ,SUM(quantity) AS total_quantity

    FROM orderhist

    WHERE productid = 2

    GROUP BY productid

    GO

  • 8/14/2019 SQL - 2071B 04

    8/16

    Using the GROUP BY Clause with theHAVING Clause

    USE northwind

    SELECT productid, orderid

    ,quantity

    FROM orderhist

    GO

    USE northwind

    SELECT productid, SUM(quantity)

    AS total_quantity

    FROM orderhist

    GROUP BY productid

    HAVING SUM(quantity)>=30

    GO

    productidproductid total_quantitytotal_quantity

    2 35

    3 45

    productidproductid orderidorderid quantityquantity1 1 5

    1 1 10

    2 1 10

    2 2 25

    3 1 15

    3 2 30

  • 8/14/2019 SQL - 2071B 04

    9/16

    Generating Aggregate ValuesWithin Result Sets

    Using the GROUP BY Clause with theROLLUP Operator

    Using the GROUP BY Clause with the

    CUBE Operator

    Using the GROUPING Function

  • 8/14/2019 SQL - 2071B 04

    10/16

    Description

    Using the GROUP BY Clause with theROLLUP OperatorUSE northwindSELECT productid, orderid, SUM(quantity) AS total_quantityFROM orderhist

    GROUP BY productid, orderidWITH ROLLUPORDER BY productid, orderidGO

    productidproductid orderidorderid total_quantitytotal_quantity

    NULL NULL 95

    1 NULL 15

    1 1 5

    1 2 10

    2 NULL 35

    2 1 102 2 25

    3 NULL 45

    3 1 15

    3 2 30

    Grand total

    Summarizes only rows for productid 1

    Detail value for productid 1, orderid 1

    Detail value for productid 1, orderid 2

    Summarizes only rows for productid 2

    Detail value for productid 2, orderid 1

    Summarizes only rows for productid 3

    Detail value for productid 3, orderid 1

    Detail value for productid 3, orderid 2

  • 8/14/2019 SQL - 2071B 04

    11/16

  • 8/14/2019 SQL - 2071B 04

    12/16

    1 represents summary valuesin the preceding column

    0 represents detail values inthe preceding column

    95

    30

    65

    15

    5

    10

    3510

    25

    45

    15

    30

    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, orderid

    GOproductidproductid

    NULL

    NULL

    NULL

    1

    1

    1

    22

    2

    3

    3

    3

    1

    1

    1

    0

    0

    0

    00

    0

    0

    0

    0

    orderidorderid

    NULL

    1

    2

    NULL

    1

    2

    NULL1

    2

    NULL

    1

    2

    1

    0

    0

    1

    0

    0

    10

    0

    1

    0

    0

    total_quantitytotal_quantity

  • 8/14/2019 SQL - 2071B 04

    13/16

  • 8/14/2019 SQL - 2071B 04

    14/16

    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 Orde

    Use the ROLLUP Operator Instead of the CUBE Operat

    Recommended Practices

  • 8/14/2019 SQL - 2071B 04

    15/16

    Lab A: Grouping and SummarizingData

  • 8/14/2019 SQL - 2071B 04

    16/16

    Review

    Listing the TOP n Values

    Using Aggregate Functions

    GROUP BY Fundamentals

    Generating Aggregate Values WithinResult Sets

    Using the COMPUTE and COMPUTE BYClauses