Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.

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.
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.
Chapter 11 Group Functions
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.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
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.
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)
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.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Chapter 12 Subqueries and Merge Statements
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
Introduction to Functions – Single Row Functions.
Course title: Database-ii Chap No: 03 “Advanced SQL” Course instructor: ILTAF MEHDI.
Module 4: Grouping and Summarizing Data. Overview Listing the TOP n Values Using Aggregate Functions GROUP BY Fundamentals Generating Aggregate Values.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
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.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Chapter 3 Introduction to SQL(3)
Aggregating Data Using Group Functions
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
SQL : Query Language Part II CS3431.
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
SQL – Entire Select.
Built in Functions Massaging the data.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
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
Chapter Name SQL: Data Manipulation
Aggregating Data Using Group Functions
Group Operations Part IV.
Presentation transcript:

Chapter 6 Group Functions

Chapter 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  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

Chapter Objectives  Use the MIN and MAX functions with non-numeric fields  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  Use the MIN and MAX functions with non-numeric fields  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

Chapter Objectives  State the maximum depth for nesting group functions  Nest a group function inside a single- row function  Calculate the standard deviation and variance of a set of data, using the STDDEV and VARIANCE functions  State the maximum depth for nesting group functions  Nest a group function inside a single- row function  Calculate the standard deviation and variance of a set of data, using the STDDEV and VARIANCE functions

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

SUM Function Calculates total amount stored in a numeric column for a group of rows

AVG Function Calculates average of numeric values in a specified column

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

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

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

MAX Function Returns largest value

MIN Function Returns smallest value

GROUP BY Clause  Used to group data  Must be used for individual column in the SELECT clause with a group function  Cannot reference column alias  Used to group data  Must be used for individual column in the SELECT clause with a group function  Cannot reference column alias

GROUP BY Example

HAVING Clause Serves as the WHERE clause for grouped data

Order of Clause Evaluation When included in the same SELECT statement, evaluated in order of: –WHERE –GROUP BY –HAVING When included in the same SELECT statement, evaluated in order of: –WHERE –GROUP BY –HAVING

Nesting Functions  Inner function resolved first  Maximum nesting depth: 2  Inner function resolved first  Maximum nesting depth: 2

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

STDDEV Function Calculates standard deviation for grouped data

VARIANCE Function Determines data dispersion within a group