5 Copyright © Oracle Corporation, 2001. All rights reserved. Aggregating Data Using 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)
5 5 Aggregating Data Using Group Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
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.
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.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
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.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
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
After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
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.
17 Copyright © Oracle Corporation, All rights reserved. Enhancements to the GROUP BY Clause.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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.
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
 After completing this lesson, you should be able to do the following: ◦ Use the ROLLUP operation to produce subtotal values ◦ Use the CUBE operation.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
4 Copyright © 2006, Oracle. All rights reserved. Generating Reports by Grouping Related Data.
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Group Functions Lab 6.
Working with Tables: Join, Functions and Grouping
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions

5-2 Copyright © Oracle Corporation, All rights reserved. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group data using the GROUP BY clause Include or exclude grouped rows by using the HAVING clause

5-3 Copyright © Oracle Corporation, All rights reserved. What Are Group Functions? Group functions operate on sets of rows to give one result per group. EMPLOYEES The maximum salary in the EMPLOYEES table. …

5-4 Copyright © Oracle Corporation, All rights reserved. Types of Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE

5-5 Copyright © Oracle Corporation, All rights reserved. SELECT[column,] group_function(column),... FROMtable [WHEREcondition] [GROUP BYcolumn] [ORDER BYcolumn]; Group Functions Syntax

5-6 Copyright © Oracle Corporation, All rights reserved. SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary) FROM employees WHERE job_id LIKE '%REP%'; Using the AVG and SUM Functions You can use AVG and SUM for numeric data.

5-7 Copyright © Oracle Corporation, All rights reserved. Using the MIN and MAX Functions You can use MIN and MAX for any data type. SELECT MIN(hire_date), MAX(hire_date) FROM employees;

5-8 Copyright © Oracle Corporation, All rights reserved. SELECT COUNT(*) FROM employees WHERE department_id = 50; Using the COUNT Function COUNT(*) returns the number of rows in a table.

5-9 Copyright © Oracle Corporation, All rights reserved. Using the COUNT Function COUNT(expr) returns the number of rows with non-null values for the expr. Display the number of department values in the EMPLOYEES table, excluding the null values. SELECT COUNT(commission_pct) FROM employees WHERE department_id = 80;

5-10 Copyright © Oracle Corporation, All rights reserved. SELECT COUNT(DISTINCT department_id) FROM employees; Using the DISTINCT Keyword COUNT(DISTINCT expr) returns the number of distinct non-null values of the expr. Display the number of distinct department values in the EMPLOYEES table.

5-11 Copyright © Oracle Corporation, All rights reserved. SELECT AVG(commission_pct) FROM employees; Group Functions and Null Values Group functions ignore null values in the column.

5-12 Copyright © Oracle Corporation, All rights reserved. SELECT AVG(NVL(commission_pct, 0)) FROM employees; Using the NVL Function with Group Functions The NVL function forces group functions to include null values.

5-13 Copyright © Oracle Corporation, All rights reserved. Creating Groups of Data EMPLOYEES The average salary in EMPLOYEES table for each department …

5-14 Copyright © Oracle Corporation, All rights reserved. SELECTcolumn, group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [ORDER BYcolumn]; Creating Groups of Data: The GROUP BY Clause Syntax Divide rows in a table into smaller groups by using the GROUP BY clause.

5-15 Copyright © Oracle Corporation, All rights reserved. SELECT department_id, AVG(salary) FROM employees GROUP BY department_id ; Using the GROUP BY Clause All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.

5-16 Copyright © Oracle Corporation, All rights reserved. Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list. SELECT AVG(salary) FROM employees GROUP BY department_id ;

5-17 Copyright © Oracle Corporation, All rights reserved. Grouping by More Than One Column EMPLOYEES “Add up the salaries in the EMPLOYEES table for each job, grouped by department. …

5-18 Copyright © Oracle Corporation, All rights reserved. SELECT department_id dept_id, job_id, SUM(salary) FROM employees GROUP BY department_id, job_id ; Using the GROUP BY Clause on Multiple Columns

5-19 Copyright © Oracle Corporation, All rights reserved. Illegal Queries Using Group Functions Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause. SELECT department_id, COUNT(last_name) FROM employees; SELECT department_id, COUNT(last_name) FROM employees; SELECT department_id, COUNT(last_name) * ERROR at line 1: ORA-00937: not a single-group group function SELECT department_id, COUNT(last_name) * ERROR at line 1: ORA-00937: not a single-group group function Column missing in the GROUP BY clause

5-20 Copyright © Oracle Corporation, All rights reserved. Illegal Queries Using Group Functions You cannot use the WHERE clause to restrict groups. You use the HAVING clause to restrict groups. You cannot use group functions in the WHERE clause. SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 8000 GROUP BY department_id; SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 8000 GROUP BY department_id; WHERE AVG(salary) > 8000 * ERROR at line 3: ORA-00934: group function is not allowed here WHERE AVG(salary) > 8000 * ERROR at line 3: ORA-00934: group function is not allowed here Cannot use the WHERE clause to restrict groups

5-21 Copyright © Oracle Corporation, All rights reserved. Excluding Group Results The maximum salary per department when it is greater than $10,000 EMPLOYEES …

5-22 Copyright © Oracle Corporation, All rights reserved. SELECTcolumn, group_function FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn]; Excluding Group Results: The HAVING Clause Use the HAVING clause to restrict groups: 1.Rows are grouped. 2.The group function is applied. 3.Groups matching the HAVING clause are displayed.

5-23 Copyright © Oracle Corporation, All rights reserved. Using the HAVING Clause SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000 ;

5-24 Copyright © Oracle Corporation, All rights reserved. SELECT job_id, SUM(salary) PAYROLL FROM employees WHERE job_id NOT LIKE '%REP%' GROUP BY job_id HAVING SUM(salary) > ORDER BY SUM(salary); Using the HAVING Clause

5-25 Copyright © Oracle Corporation, All rights reserved. Nesting Group Functions Display the maximum average salary. SELECT MAX(AVG(salary)) FROM employees GROUP BY department_id;

5-26 Copyright © Oracle Corporation, All rights reserved. SELECTcolumn, group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn]; Summary In this lesson, you should have learned how to: Use the group functions COUNT, MAX, MIN, AVG Write queries that use the GROUP BY clause Write queries that use the HAVING clause

5-27 Copyright © Oracle Corporation, All rights reserved. Practice 5 Overview This practice covers the following topics: Writing queries that use the group functions Grouping by rows to achieve more than one result Excluding groups by using the HAVING clause

5-28 Copyright © Oracle Corporation, All rights reserved.

5-29 Copyright © Oracle Corporation, All rights reserved.

5-30 Copyright © Oracle Corporation, All rights reserved.