0% found this document useful (0 votes)
23 views35 pages

Oracle 12c SQL: Group Functions Guide

The document outlines the objectives and functions related to group functions in Oracle 12c SQL, including the use of SUM, AVG, COUNT, MIN, and MAX functions, as well as the GROUP BY and HAVING clauses. It emphasizes the importance of understanding how to group data, restrict aggregated output, and perform statistical analyses using functions like STDDEV and VARIANCE. Additionally, it discusses advanced aggregation techniques such as GROUPING SETS, CUBE, and ROLLUP for enhanced reporting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views35 pages

Oracle 12c SQL: Group Functions Guide

The document outlines the objectives and functions related to group functions in Oracle 12c SQL, including the use of SUM, AVG, COUNT, MIN, and MAX functions, as well as the GROUP BY and HAVING clauses. It emphasizes the importance of understanding how to group data, restrict aggregated output, and perform statistical analyses using functions like STDDEV and VARIANCE. Additionally, it discusses advanced aggregation techniques such as GROUPING SETS, CUBE, and ROLLUP for enhanced reporting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

About the Presentations

• The presentations cover the objectives found in the


opening of each chapter.
• All chapter objectives are listed in the beginning of
each presentation.
• You may customize the presentations to fit your
class needs.
• Some figures from the chapters are included. A
complete set of images from the book can be found
on the Instructor Resources disc.
©2016. Cengage Learning. All rights reserved.
Oracle 12c: SQL

Chapter 11
Group Functions

©2016. Cengage Learning. All rights reserved.


Objectives
• Differentiate between single-row and multiple-
row functions
• Use the SUM and AVG functions for numeric
calculations
• Use the COUNT function to return the number
of records containing non-NULL values
• Use COUNT(*) to include records containing
NULL values
• Use the MIN and MAX functions with
nonnumeric fields
©2016. Cengage Learning. All rights reserved. 3
Objectives (continued)

• Determine when to use the GROUP BY clause


to group data
• Identify when the HAVING clause should be
used
• List the order of precedence for evaluating
WHERE, GROUP BY, and HAVING clauses
• State the maximum depth for nesting group
functions
• Nest a group function inside of a single-row
function
©2016. Cengage Learning. All rights reserved. 4
Objectives (continued)
• Calculate the standard deviation and variance
of a set of data, using the STDDEV and
VARIANCE functions
• Explain the concept of multidimensional
analysis
• Perform enhanced aggregation grouping with
the GROUPING SETS, CUBE, and ROLLUP
• Use composite columns and concatenated
groupings in grouping operations

©2016. Cengage Learning. All rights reserved. 5


Group Functions
• Return one result per group of rows processed
• Are also called multiple-row and aggregate
functions
• All group functions ignore NULL values except
COUNT(*)
• Use DISTINCT to suppress duplicate values

©2016. Cengage Learning. All rights reserved. 6


Added Clauses

©2016. Cengage Learning. All rights reserved. 7


SUM Function

• Calculates total amount stored in a numeric


column for a group of rows

©2016. Cengage Learning. All rights reserved. 8


AVG Function

• Calculates the average of numeric values in


a specified column

©2016. Cengage Learning. All rights reserved. 9


COUNT Function
• Two purposes
– Count non-NULL values
– Count total records, including those with NULL
values

©2016. Cengage Learning. All rights reserved. 10


COUNT Function –
Non-NULL Values
• Include column name in argument to count
number of occurrences

©2016. Cengage Learning. All rights reserved. 11


COUNT Function –
NULL Values
• Include asterisk in argument to count number
of rows

©2016. Cengage Learning. All rights reserved. 12


MAX Function

• Returns largest value

©2016. Cengage Learning. All rights reserved. 13


MIN Function

• Returns the smallest value

©2016. Cengage Learning. All rights reserved. 14


Datatypes
• The COUNT, MIN, and MAX functions can be used
on values with character, numeric, and date
datatypes

©2016. Cengage Learning. All rights reserved. 15


Grouping Data
• GROUP BY clause
– Used to group data
– Must be used for any individual column in the
SELECT clause with a group function
– Cannot reference column aliases

©2016. Cengage Learning. All rights reserved. 16


GROUP BY Example

©2016. Cengage Learning. All rights reserved. 17


Common Error
• A common
error is missing
a GROUP BY
clause for
nonaggregated
columns in the
SELECT clause

©2016. Cengage Learning. All rights reserved. 18


Restricting Aggregated Output

• HAVING clause serves as the WHERE clause for


grouped data

©2016. Cengage Learning. All rights reserved. 19


Restricting Aggregated Output
(continued)
• When included in the same SELECT statement,
the clauses are evaluated in the order of:
– WHERE
– GROUP BY
– HAVING

©2016. Cengage Learning. All rights reserved. 20


Restricting Aggregated Output
(continued)

©2016. Cengage Learning. All rights reserved. 21


Nesting Functions

• Inner function is resolved first


• Maximum nesting depth: 2

©2016. Cengage Learning. All rights reserved. 22


Statistical Group Functions
• Based on normal distribution
• Includes:
– STDDEV
– VARIANCE

©2016. Cengage Learning. All rights reserved. 23


STDDEV Function

©2016. Cengage Learning. All rights reserved. 24


VARIANCE Function
• Determines data dispersion within a group

©2016. Cengage Learning. All rights reserved. 25


Enhanced Aggregation for Reporting
• Oracle provides extensions to the GROUP BY
clause, which allow both aggregation across
multiple dimensions or the generation of
increasing levels of subtotals with a single
SELECT statement
• A dimension is a term used to describe any
category used in analyzing data, such as time,
geography, and product line
• Each dimension could contain various levels of
aggregation; for example, the time dimension
may include aggregation by month, quarter,
and year
©2016. Cengage Learning. All rights reserved. 26
Excel Pivot Table Example

©2016. Cengage Learning. All rights reserved. 27


Excel Pivot Table Example (continued)

©2016. Cengage Learning. All rights reserved. 28


Grouping Sets

©2016. Cengage Learning. All rights reserved. 29


CUBE

©2016. Cengage Learning. All rights reserved. 30


ROLLUP

©2016. Cengage Learning. All rights reserved. 31


Pattern Matching

©2016. Cengage Learning. All rights reserved. 32


Summary
• The AVG, SUM, STDDEV, and VARIANCE
functions are used only with numeric fields
• The COUNT, MAX, and MIN functions can be
applied to any datatype
• The AVG, SUM, MAX, MIN, STDDEV, and
VARIANCE functions all ignore NULL values
• By default, the AVG, SUM, MAX, MIN, COUNT,
STDDEV, and VARIANCE functions include
duplicate values

©2016. Cengage Learning. All rights reserved. 33


Summary (continued)
• The GROUP BY clause is used to divide table data into
groups
• 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
specified grouping. The results of the inner function are
used as input for the outer function.

©2016. Cengage Learning. All rights reserved. 34


Summary (continued)
• The STDDEV and VARIANCE functions are used to perform
statistical analyses on a set of data
• GROUPING SETS operations can be used to perform multiple
GROUP 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
©2016. Cengage Learning. All rights reserved. 35

You might also like