Chapter 11 Group Functions

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Chapter 11 Group Functions (up to p.402)
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Structured Query Language Part I Chapter Three CIS 218.
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
Concepts of Database Management, Fifth Edition
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
1 SQL-3 Tarek El-Shishtawy Professor Ass. Of Computer Engineering.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 12 Subqueries and Merge Statements
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Introduction to Functions – Single Row Functions.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Query Functions.
Section 4 - Sorting/Functions
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Group Operations Part IV.
Presentation transcript:

Chapter 11 Group Functions Oracle 11g: SQL Chapter 11 Group Functions

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 Oracle 11g: SQL

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 Oracle 11g: SQL

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 Oracle 11g: SQL

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 Oracle 11g: SQL

Added Clauses Oracle 11g: SQL

SUM Function Calculates total amount stored in a numeric column for a group of rows Oracle 11g: SQL

AVG Function Calculates the average of numeric values in a specified column Oracle 11g: SQL

COUNT Function Two purposes Count non-NULL values Count total records, including those with NULL values Oracle 11g: SQL

COUNT Function – Non-NULL Values Include column name in argument to count number of occurrences Oracle 11g: SQL

COUNT Function – NULL Values Include asterisk in argument to count number of rows Oracle 11g: SQL

MAX Function Returns largest value Oracle 11g: SQL

MIN Function Returns the smallest value Oracle 11g: SQL

Datatypes The COUNT, MIN, and MAX functions can be used on values with character, numeric, and date datatypes Oracle 11g: SQL

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 Oracle 11g: SQL

GROUP BY Example Oracle 11g: SQL

Common Error A common error is missing a GROUP BY clause for nonaggregated columns in the SELECT clause Oracle 11g: SQL

Restricting Aggregated Output HAVING clause serves as the WHERE clause for grouped data Oracle 11g: SQL

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

Restricting Aggregated Output (continued) Oracle 11g: SQL

Nesting Functions Inner function is resolved first Maximum nesting depth: 2 Oracle 11g: SQL

Statistical Group Functions Based on normal distribution Includes: STDDEV VARIANCE Oracle 11g: SQL

STDDEV Function Oracle 11g: SQL

VARIANCE Function Determines data dispersion within a group Oracle 11g: SQL

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 Oracle 11g: SQL

Excel Pivot Table Example Oracle 11g: SQL

Excel Pivot Table Example (continued) Oracle 11g: SQL

Grouping Sets Oracle 11g: SQL

CUBE Oracle 11g: SQL

ROLLUP Oracle 11g: SQL

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 Oracle 11g: SQL

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. Oracle 11g: SQL

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 Oracle 11g: SQL