Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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 www.oracle.com  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.

2 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

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

4 Types of Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE

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

6 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) ---------------------------------------------------------- 1400160012505600 You can use AVG and SUM for numeric data.

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

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

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

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

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

12 Creating Groups of Data DEPTNOSAL ------------ ------------------- 102450 105000 101300 20800 201100 203000 203000 202975 301600 302850 301250 30950 301500 301250 “average salary in EMP table for each department” 2649.6667 2175 1566.6667 DEPTNOAVG(SAL) ------------ --------------- 102916.6667 202175 301566.6667

13 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.

14 Using the GROUP BY Clause DEPTNOAVG(SAL) -------------------------- 10 2916.6667 20 2175 301566.6667 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.

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

16 Grouping by More Than One Column DEPTNOJOBSAL -------------------- --------- 10MANAGER2450 10PRESIDENT5000 10CLERK1300 20CLERK800 20CLERK1100 20ANALYST3000 20ANALYST3000 20MANAGER2850 30SALESMAN1250 30MANAGER2850 30SALESMAN1250 30CLERK950 30SALESMAN1500 30SALESMAN1250... 14 rows selected. EMP “ sum salaries in the EMP table for each job, grouped by department.” DEPTNOJOBSUM(SAL) ------------ ------------ -------------- 10CLERK 1300 10MANAGER 2450 10PRESIDENT 5000 20ANALYST 6000 20CLERK 1900 20MANAGER 2975 30CLERK 950 30MANAGER 2850 30SALESMAN 5600

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

18 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

19 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) > 2000 4GROUP BYdeptno; WHEREAVG (sal) > 2000 * ERROR at line 3: ORA-00934: group function is not allowed here Cannot use the WHERE clause to restrict groups

20 Excluding Group Results DEPTNOSAL ------------------------------ 102450 105000 101300 20800 201100 203000 203000 202975 301600 302850 301250 30950 301500 301250 “maximum salary per department greater than $2900” 5000 3000 2850 DEPTNOMAX(SAL) ------------ --------------- 105000 203000

21 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];

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

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

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

25 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

26 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


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

Similar presentations


Ads by Google