Oracle_ch11.pptx

download Oracle_ch11.pptx

of 49

Transcript of Oracle_ch11.pptx

  • 8/10/2019 Oracle_ch11.pptx

    1/49

    Dr. Chen, Oracle Database System (Oracle) 1

    Chapter 11Group Functions

    (up to p.402)

    Jason C. H. Chen,Ph.D.

    Professor of MIS

    School of Business

    Gonzaga University

    Spokane, WA 99258 USA

    [email protected]

  • 8/10/2019 Oracle_ch11.pptx

    2/49

    Dr. Chen, Oracle Database System (Oracle) 2

    Objectives

    Differentiate between single-row and multiple-rowfunctions

    Use the SUM and AVG functions for numericcalculations

    Use the COUNT function to return the number ofrecords containing non-NULL values

    Use COUNT(*) to include records containingNULL values

    Use the MIN and MAX functions withnonnumeric fields

  • 8/10/2019 Oracle_ch11.pptx

    3/49

    Dr. Chen, Oracle Database System (Oracle) 3

    Objectives (continued)

    Determine when to use the GROUP BY clause togroup data

    Identify when the HAVING clause should be used

    List the order of precedence for evaluatingWHERE, GROUP BY, and HAVING clauses

    State the maximum depth for nesting group

    functions

    Nest a group function inside of a single-row

    function

  • 8/10/2019 Oracle_ch11.pptx

    4/49

    Dr. Chen, Oracle Database System (Oracle) 4

    Objectives (continued)

    Calculate the standard deviation and variance of aset of data, using the STDDEV and VARIANCEfunctions

    Explain the concept of multidimensional analysis

    Perform enhanced aggregation grouping with theGROUPING SETS, CUBE, and ROLLUP

    Use composite columns and concatenatedgroupings in grouping operations

  • 8/10/2019 Oracle_ch11.pptx

    5/49

    Dr. Chen, Oracle Database System (Oracle) 5

    Refresh the Database

    1. Download chapter 11 files from Bb toc:\oradata\chapter11\

    2. Run the following script file

    Start c:\oradata\chapter11\JLDB_Build_11.sql

  • 8/10/2019 Oracle_ch11.pptx

    6/49

    Dr. Chen, Oracle Database System (Oracle) 6

    Group Functions

    Return one result per group of rowsprocessed

    Are also called multiple-row and aggregate

    functions All group functions ignore NULL values

    except COUNT(*)

    Use DISTINCT to suppress duplicate values

  • 8/10/2019 Oracle_ch11.pptx

    7/49

    Dr. Chen, Oracle Database System (Oracle) 7

    Added Clauses

    Figure 11-1 SELECT statement syntax

  • 8/10/2019 Oracle_ch11.pptx

    8/49

    Dr. Chen, Oracle Database System (Oracle) 8

    SUM Function

    Calculates total amount stored in a numericcolumn for a group of rows

    Figure 11-2 Using the SUM function to calculate order profit

  • 8/10/2019 Oracle_ch11.pptx

    9/49

    Dr. Chen, Oracle Database System (Oracle) 9

    AVG Function

    Calculates the average of numeric values in a

    specified column

    Figure 11-4 Using the AVG function to calculate average profit

  • 8/10/2019 Oracle_ch11.pptx

    10/49

    Dr. Chen, Oracle Database System (Oracle) 10

    -- chapter 11, Figure 11-5(B); p.389

    SELECT TO_CHAR(AVG( retail - cost), '$999.99') "Average Profit"

    FROM books

    WHERE category = 'COMPUTER';

    Figure 11-4 Using the AVG function to calculate average profit

  • 8/10/2019 Oracle_ch11.pptx

    11/49

    Dr. Chen, Oracle Database System (Oracle) 11

    COUNT Function

    Two purposesCount non-NULL values

    Count total records, including those with NULL

    values

  • 8/10/2019 Oracle_ch11.pptx

    12/49

    Dr. Chen, Oracle Database System (Oracle) 12

    COUNT FunctionNon-NULL Values

    Include column name in argument to countnumber of occurrences

    Figure 11-9 Using the COUNT function with the DISTINCT option

  • 8/10/2019 Oracle_ch11.pptx

    13/49

    Dr. Chen, Oracle Database System (Oracle) 13

    COUNT FunctionNULL Values

    Include asterisk in argument to count number of rows

    Figure 11-11 Using the COUNT(*) function to include NULL values

  • 8/10/2019 Oracle_ch11.pptx

    14/49

    Dr. Chen, Oracle Database System (Oracle) 14

    MAX Function

    Returns largest value

    Figure 11-13 Using the MAX function on numeric data

  • 8/10/2019 Oracle_ch11.pptx

    15/49

    Dr. Chen, Oracle Database System (Oracle) 15

    MIN Function

    Returns the smallest value

    Figure 11-16 Using the MIN function on date data

  • 8/10/2019 Oracle_ch11.pptx

    16/49

    Dr. Chen, Oracle Database System (Oracle) 16

    Datatypes

    The COUNT, MIN, and MAX functionscan be used on values with character,

    numeric, and date datatypes

  • 8/10/2019 Oracle_ch11.pptx

    17/49

    Dr. Chen, Oracle Database System (Oracle) 17

    Grouping Data

    GROUP BY clauseUsed to group data

    Must be used for any individual column in the

    SELECT clause with a group functionCannot reference column aliases

  • 8/10/2019 Oracle_ch11.pptx

    18/49

    Dr. Chen, Oracle Database System (Oracle) 18

    GROUP BY Example

    Figure 11-18 Adding the GROUP BY clause

  • 8/10/2019 Oracle_ch11.pptx

    19/49

    Dr. Chen, Oracle Database System (Oracle) 19

    Common Error

    A common

    error ismissing a

    GROUP BY

    clause for

    nonaggregate

    d columns in

    the SELECT

    clause

    Figure 11-17 Flawed query: Including both aggregateand

    nonaggregate columns requires a GROUP BY clause

  • 8/10/2019 Oracle_ch11.pptx

    20/49

    Dr. Chen, Oracle Database System (Oracle) 20

    Figure 11-19 Inappropriate use of GROUP BY

    It is an individual

    record (just Profit

    not Highest Profit)

  • 8/10/2019 Oracle_ch11.pptx

    21/49

    Dr. Chen, Oracle Database System (Oracle) 21

    Figure 11-20 Calculate

    the total amount due by

    each customer and

    order

  • 8/10/2019 Oracle_ch11.pptx

    22/49

    Dr. Chen, Oracle Database System (Oracle) 22

    Restricting Aggregated Output

    HAVING clause serves as the WHERE clause for grouped data.

    It is used to eliminatecertain groups from further consideration.

    Figure 11-21 Using a HAVING clause to restrict which groups are displayed

    4 records are eliminated

  • 8/10/2019 Oracle_ch11.pptx

    23/49

    Dr. Chen, Oracle Database System (Oracle) 23

    Restricting Aggregated Output (continued)

    When included in the same SELECTstatement, the clauses are evaluated in the

    order of:

    WHEREGROUP BY

    HAVING

  • 8/10/2019 Oracle_ch11.pptx

    24/49

    Dr. Chen, Oracle Database System (Oracle) 24

    Restricting Aggregated Output (continued)

    Figure 11-22 Using the WHERE, GROUP BY, and HAVING clauses

  • 8/10/2019 Oracle_ch11.pptx

    25/49

    Dr. Chen, Oracle Database System (Oracle) 25

    Figure 11-23 Using a HAVING clause to

    restrict grouped output

  • 8/10/2019 Oracle_ch11.pptx

    26/49

    Dr. Chen, Oracle Database System (Oracle) 26

    Figure 11-24 Filtering correctly

    with the WHERE and HAVING

    clauses

  • 8/10/2019 Oracle_ch11.pptx

    27/49

    Dr. Chen, Oracle Database System (Oracle) 27

    Figure 11-25 Filtering

    incorrectly with the HAVING

    clauses

    It is quite inefficient and

    considered poor SQL

    programming practice. The

    statement must process

    ALL rows in the BOOKS

    table with the aggregated

    calculation and then

    eliminate categories.

    Figure 11-24 Filtering correctly with the WHERE and HAVING clauses

  • 8/10/2019 Oracle_ch11.pptx

    28/49

    Dr. Chen, Oracle Database System (Oracle) 28

    Nesting Functions

    Inner function is resolved first

    Maximum nesting depth: 2

    Figure 11-26 Nesting group functions

  • 8/10/2019 Oracle_ch11.pptx

    29/49

    Dr. Chen, Oracle Database System (Oracle) 29

    Exercises

    Practice all the examples in the text. A Script file is available on the Bb (file

    name: ch11Queries.sql)

    After completing all examples, do the HW.

    In-class Exercise

    #7 (p.424)

  • 8/10/2019 Oracle_ch11.pptx

    30/49

    Dr. Chen, Oracle Database System (Oracle) 30

    Homework - Hands-On Assignments

    Read and Practice all examples on Chapters 11

    1. Run the script files (in the folder \oradata\chapter11\):

    JLDB_Build_11.sql 2. Read Oracle assignment and create a script file

    Oracle_ch11_Lname_Fname.sql for questions (ALLEVEN problems; pp.424-425) on Hands-onAssignments. Use appropriate COLUMN or otherSQL commands to produce readable outputs (or yourgrade wil l be discountedsee a sample output on the Bb)

    3. Execute and test one problem at a time and make surethey are all running successfully.

    4. When you done, spool the script files (see next slide forspooling instructions) and email the file(Oracle_ch11_Spool_Lname_Fname.txt) to me by themidnight before the next class.

    Email me with one attachment

    (Oracle_ch11_Spool_Lname_Fname.) to:

    [email protected] subject title of

    bmis441_Oracle_ch11

  • 8/10/2019 Oracle_ch11.pptx

    31/49

    Dr. Chen, Oracle Database System (Oracle) 31

    How to Spool your Script and Output FilesAfter you tested the script file of Oracle_ch11_Lname_Fname.sql

    successfully, follow the instructions below to spool both script and outputfiles:

    Step 0. Run the following script file from SQL*Plus (since you have createdJLDB tables)

    Start c:\oradata\chapter11\JLDB_Build_11.sql

    1. type the following on SQL> Spool c:\oradata\Oracle_ch11_Spool_Lname_Fname.txt(make sure your name is

    entered) 2. open Oracle_ch11_Lname_Fname.sql that you already tested

    3. copy and paste all the SQL commands (including all comments) to theSQL*PLUS

    4. type Spool Off on the SQL>

    The output should contain your personal information, all SQL commands andtheir solution on the .txt file and saved in C: drive (oradata\ folder)Email me with the spooled file (.txt) with attachment to:

    [email protected]

    with subject title of

    bmis441_Oracle_ch11

  • 8/10/2019 Oracle_ch11.pptx

    32/49

    Dr. Chen, Oracle Database System (Oracle) 32

    Summary

    The AVG, SUM, STDDEV, and VARIANCEfunctions are used only with numeric fields

    The COUNT, MAX, and MIN functions can beapplied to any datatype

    The AVG, SUM, MAX, MIN, STDDEV, andVARIANCE functions all ignore NULL values

    By default, the AVG, SUM, MAX, MIN,COUNT, STDDEV, and VARIANCE functions

    include duplicate values

  • 8/10/2019 Oracle_ch11.pptx

    33/49

    Dr. Chen, Oracle Database System (Oracle) 33

    Summary (continued)

    The GROUP BY clause is used to divide table data intogroups

    If a SELECT clause contains both an individual field name

    and a group function, the field name must also be included in

    a GROUP BY clause The HAVING clause is used to restrict groups in a group

    function

    Group functions can be nested to a depth of only two. The

    inner function is always performed first, using the specifiedgrouping. The results of the inner function are used as input

    for the outer function.

  • 8/10/2019 Oracle_ch11.pptx

    34/49

    Dr. Chen, Oracle Database System (Oracle) 34

    Summary (continued)

    The STDDEV and VARIANCE functions are used to performstatistical analyses on a set of data

    GROUPING SETS operations can be used to perform multipleGROUP BY aggregations with a single query

    The CUBE extension of the GROUP BY calculates aggregations

    for all possible combinations or groupings of columns included The ROLLUP extension of the GROUP BY calculates increasing

    levels of accumulated subtotals for the column list provided

    Composite columns and concatenated groupings can be used in

    GROUPING SETS, CUBE, and ROLLUP operations

    The GROUP_ID function helps eliminate duplicate grouping

    results

  • 8/10/2019 Oracle_ch11.pptx

    35/49

    Dr. Chen, Oracle Database System (Oracle) 35

    STDDEV Function

    Figure 11-27 Using the STDDEV function

  • 8/10/2019 Oracle_ch11.pptx

    36/49

    Dr. Chen, Oracle Database System (Oracle) 36

    VARIANCE Function

    Determines data dispersion within a group

    Figure 11-28 Using the VARIANCE function

  • 8/10/2019 Oracle_ch11.pptx

    37/49

    Dr. Chen, Oracle Database System (Oracle) 37

    Enhanced Aggregation for Reporting

    Oracle provides extensions to the GROUP BYclause, which allow both aggregationacrossmultiple dimensions or thegenerationofincreasing levels of subtotals with a singleSELECT statement

    A dimensionis a term used to describe anycategory used in analyzing data, such as time,geography, and product line

    Each dimension could contain various levels of

    aggregation; for example, the time dimension mayinclude aggregation by month, quarter, and year;the product dimension may include product type,sales, store, region and month.

  • 8/10/2019 Oracle_ch11.pptx

    38/49

    Dr. Chen, Oracle Database System (Oracle) 38

    Figure Slicing a data cube

    CUSTOMER

    REGION

  • 8/10/2019 Oracle_ch11.pptx

    39/49

    Dr. Chen, Oracle Database System (Oracle) 39

    Excel Pivot Table Example

    Figure 11-30 A pivot table with two dimensions on a row

  • 8/10/2019 Oracle_ch11.pptx

    40/49

    Dr. Chen, Oracle Database System (Oracle) 40

    Excel Pivot Table Example (continued)

    Figure 11-31 A pivot table with one row and one column dimension

  • 8/10/2019 Oracle_ch11.pptx

    41/49

    Dr. Chen, Oracle Database System (Oracle) 41

    GROUPING SETS

    The grouping sets expression is the component on which

    the other GROUP BY extensions, ROLLUP and CUBE,are built.

    With this extension, you can use a single query statement

    to perform multiple GROUP BY clauses.

    The single query in Figure 11-32 produces the averageretail price for books infourgroupings: 1) publisher (the

    Name column) and category, 2) category, 3) publisher,

    and 4) overall average.

    -- chapter 11, Figure 11-32; p.408

    SELECT name, category, COUNT(isbn),

    TO_CHAR(AVG(retail), '99.99') "Avg Retail"

    FROM publisher JOIN books USING (pubid)

    WHERE pubid IN (2,3,5)

    GROUP BY GROUPING SETS (name, category, (name, category), ())

    Try withoutGROUING

    SETS

  • 8/10/2019 Oracle_ch11.pptx

    42/49

    Dr. Chen, Oracle Database System (Oracle) 42

    Without GROUPING SETS

    -- chapter 11, Figure 11-32(a); p.408SELECT name, category, COUNT(isbn),

    TO_CHAR(AVG(retail), '99.99') "Avg Retail"

    FROM publisher JOIN books USING (pubid)

    WHERE pubid IN (2,3,5)

    GROUP BY name, category;

    G i S

  • 8/10/2019 Oracle_ch11.pptx

    43/49

    Dr. Chen, Oracle Database System (Oracle) 43

    Grouping Sets

    Figure 11-32 Using a GROUPING SETS expression in a GROUP BY clause

  • 8/10/2019 Oracle_ch11.pptx

    44/49

    Dr. Chen, Oracle Database System (Oracle) 44

    The CUBE Extension

    The CUBE extension of GROUP BY instructs Oracle to

    perform aggregations for all possible combinations ofthe specified columns (e.g., two columns: Name

    (publisher name) and Category).

    The outputs matches Figure 11-32.

    If you need only asubset of the four aggregate levels

    calculated, you must use the GROUPING SETS

    expression because the CUBE extension always

    performs all aggregation levels.

    Adding a GROUPING function to the CUBE extension

    (Fig. 11-35) to identifysubtotalrows in the results

    (helpful in labeling sorting, and restricting output).

  • 8/10/2019 Oracle_ch11.pptx

    45/49

    Dr. Chen, Oracle Database System (Oracle) 45

    CUBE

    Figure 11-34 Using the CUBE extension of GROUP BY

  • 8/10/2019 Oracle_ch11.pptx

    46/49

    Dr. Chen, Oracle Database System (Oracle) 46Figure 11-35 The GROUPING function returns a 1 to identify subtotal rows

  • 8/10/2019 Oracle_ch11.pptx

    47/49

    Dr. Chen, Oracle Database System (Oracle) 47

    The ROLLUP Extension

    The ROLLUP extension of GROUP BY calculatescumulative subtotals for the specified columns.

    If multiple columns are indicated, subtotals are

    performed for each column in the argument list,

    except the one on the far right. Agrandtotal is

    also calculated.

    ROLLUP

  • 8/10/2019 Oracle_ch11.pptx

    48/49

    Dr. Chen, Oracle Database System (Oracle) 48

    ROLLUP

    Figure 11-37 Using the ROLLUP extension of GROUP BY

  • 8/10/2019 Oracle_ch11.pptx

    49/49

    Figure 11-38 Using a partial ROLLUP

    Category is the

    column outside the

    ROLLUPand is

    considered the

    aggregate value.A subtotal is

    calculated for the

    aggregate value as

    well as for each

    unique value of the

    ROLLUP column inthe aggregate value

    i.e., by Category

    and each Name in

    each Category .