5 5 Aggregating Data Using Group Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,

Slides:



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

2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
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.
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.
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 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Chapter 1 Writing Basic SQL Statements Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
17 Copyright © Oracle Corporation, All rights reserved. Enhancements to the GROUP BY Clause.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
13 Other Database Objects Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
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 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Subqueries.
Subqueries.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
SQL. Relating Multiple Tables Relational Database Terminology Row PK Column FK Field NULL.
4 Displaying Data from Multiple Tables Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are sourced from a book titled “Oracle Education” by Kochhar, Gravina,
 After completing this lesson, you should be able to do the following: ◦ Use the ROLLUP operation to produce subtotal values ◦ Use the CUBE operation.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
4 Copyright © 2006, Oracle. All rights reserved. Generating Reports by Grouping Related Data.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Defining a Column Alias
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Reporting Aggregated Data Using the Group Functions
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Database Programming Using Oracle 11g
Aggregating Data Using Group Functions
Presentation transcript:

5 5 Aggregating Data Using Group Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp.  For further information, visit  This presentation must be used for only education purpose for students at Central Washington University which is a member of Oracle Academic Initiatives (OAI) and has used Oracle systems for HRIS & Accounting Systems as a database platform embedded on its PeopleSoft ERP system, since 1999.

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

What are Group Functions? “maximum salary in the EMP table” MAX(SAL) Group functions operate on sets of rows to give one result per group.

Types of Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE

Using Group Functions SELECT[column,] group_function(column) FROMtable [WHEREcondition] [GROUP BYcolumn] [HAVING ….] [ORDER BYcolumn];

Using AVG and SUM Functions SQL>SELECTAVG(sal), MAX(sal), MIN(sal), SUM(sal) 3FROMemp 4WHEREjob LIKE ‘SALES%’; AVG(SAL)MAX(SAL)MIN(SAL)SUM(SAL) You can use AVG and SUM for numeric data.

Using MIN and MAX Functions SQL>SELECTMIN(hiredate), MAX(hiredate) 2FROMemp; MIN(HIREDMAX(HIRED DEC-8012-JAN-83 You can use MIN and MAX for any datatype.

Using the COUNT Function COUNT(*) SQL>SELECTCOUNT(*) 2FROMemp 3WHEREdeptno = 30; COUNT (*) returns the number of rows in a table.

Using the COUNT Function COUNT(COMM) SQL>SELECTCOUNT(comm) 2FROMemp 3WHEREdeptno = 30; COUNT(expr) returns the number of non-null rows.

Group Functions and Null Values SQL>SELECTAVG(comm) 2FROM emp; AVG(COMM) Group functions ignore null values in the column.

Using the NVL Function with Group Functions SQL>SELECTAVG(NVL(comm,0)) 2FROMemp; AVG(NVL(COMM,0)) The NVL function forces group functions to include null values.

Creating Groups of Data DEPTNOSAL “average salary in EMP table for each department” DEPTNOAVG(SAL)

Creating Groups of Data: GROUP BY Clause SELECT[column,] group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [ORDER BYcolumn]; Divide rows in a table into smaller groups by using the GROUP BY clause.

Using the GROUP BY Clause DEPTNOAVG(SAL) SQL>SELECTdeptno, AVG(sal) 2FROMemp 3GROUP BYdeptno; All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.

Using the GROUP BY Clause AVG(SAL) SQL>SELECTAVG(sal) 2FROMemp 3GROUP BYdeptno; The GROUP BY column does not have to be in the SELECT list.

Grouping by More Than One Column DEPTNOJOBSAL MANAGER PRESIDENT CLERK CLERK800 20CLERK ANALYST ANALYST MANAGER SALESMAN MANAGER SALESMAN CLERK950 30SALESMAN SALESMAN rows selected. EMP “ sum salaries in the EMP table for each job, grouped by department.” DEPTNOJOBSUM(SAL) CLERK MANAGER PRESIDENT ANALYST CLERK MANAGER CLERK MANAGER SALESMAN 5600

Using the GROUP BY Clause on Multiple Columns DEPTNOJOB CLERK 10MANAGER 10PRESIDENT 20ANALYST 20CLERK... 9 rows selected. SQL>SELECTdeptno, job, sum(sal) 2FROMemp 3GROUP BYdeptno, job; SUM(SAL)

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. SQL>SELECTdeptno, COUNT(ename) 2FROMemp; SELECTdeptno, COUNT(ename) * ERROR at line 1: ORA-00937: not a single-group group function Column is missing in the GROUP BY clause

Illegal Queries Using Group Functions You cannot use the WHERE clause to restrict groups. You use the HAVING clause to restrict groups. SQL>SELECTdeptno, AVG(sal) 2FROMemp 3WHEREAVG(sal) > GROUP BYdeptno; WHEREAVG (sal) > 2000 * ERROR at line 3: ORA-00934: group function is not allowed here Cannot use the WHERE clause to restrict groups

Excluding Group Results DEPTNOSAL “maximum salary per department greater than $2900” DEPTNOMAX(SAL)

Excluding Group Results: HAVING Clause Use the HAVING clause to restrict groups Rows are grouped. The group function is applied. Groups matching the HAVING clause are displayed. SELECTcolumn, group_function FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn];

Using the HAVING Clause MAX(SAL) SQL>SELECTdeptno, max(sal) 2FROMemp 3GROUP BYdeptno 4HAVINGmax(sal)>2900; DEPTNO

Using the HAVING Clause SQL>SELECTjob, SUM(sal) PAYROLL 2FROMemp 3WHEREjob NOT LIKE ‘SALES%’ 4GROUP BYjob 5HAVINGSUM(sal) > ORDER BYSUM(sal); JOB ANALYST MANAGER PAYROLL

Nesting Group Functions MAX(AVG(SAL) ) SQL>SELECTmax(avg(sal)) 2FROMemp 3GROUP BYdeptno; Display the maximum average salary.

Summary SELECTcolumn, group_function(column) FROMtable [WHEREcondition] [GROUP BYgroup_by_expression] [HAVINGgroup_condition] [ORDER BYcolumn]; Order of evaluation of the clauses: WHERE clause GROUP BY clause HAVING clause

Practice Overview Showing different queries that use group functions Grouping by rows to achieve more than one result Excluding groups by using the HAVING clause