sql05 - 5 Aggregating Data Using Group Functions Objectives...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5 Aggregating Data Using Group Functions Objectives After completing this lesson, you should After be able to do the following: be • 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 -2 What Are Group Functions? Group functions operate on sets of rows to give Group one result per group. one EMP DEPTNO SAL --------- --------10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 5 -3 “maximum maximum salary in the EMP table” the MAX(SAL) --------5000 Types of Group Functions • AVG • COUNT • MAX • MIN • STDDEV • SUM • VARIANCE 5 -4 Using Group Functions SELECT FROM [WHERE [ORDER BY 5 -5 column, group_function(column) table condition] column]; Using AVG and SUM Functions You can use AVG and SUM for numeric data. SQL> SELECT 2 3 FROM 4 WHERE AVG(sal), MAX(sal), MIN(sal), SUM(sal) emp job LIKE 'SALES%'; AVG(SAL) MAX(SAL) MIN(SAL) SUM(SAL) -------- --------- --------- --------1400 1600 1250 5600 5 -6 Using MIN and MAX Functions You can use MIN and MAX for any datatype. SQL> SELECT 2 FROM MIN(hiredate), MAX(hiredate) emp; MIN(HIRED MAX(HIRED --------- --------17-DEC-80 12-JAN-83 5 -7 Using the COUNT Function COUNT(*) returns the number of rows in a COUNT(*) table. table. SQL> SELECT 2 FROM 3 WHERE COUNT(*) --------6 5 -8 COUNT(*) emp deptno = 30; Using the COUNT Function COUNT(expr) returns the number of returns nonnull rows. nonnull SQL> SELECT 2 FROM 3 WHERE COUNT(COMM) ----------4 5 -9 COUNT(comm) emp deptno = 30; Group Functions and Null Values Group functions ignore null values in the Group column. column. SQL> SELECT AVG(comm) 2 FROM emp; AVG(COMM) --------550 5-10 Using the NVL Function with Group Functions The NVL function forces group functions The to include null values. to SQL> SELECT AVG(NVL(comm,0)) 2 FROM emp; AVG(NVL(COMM,0)) ---------------157.14286 5-11 Creating Groups of Data EMP EMP DEPTNO SAL --------- --------10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 5-12 2916.6667 2916.6667 “average DEPTNO AVG(SAL) salary salary ------- --------in EMP in 2175 2175 10 2916.6667 table table 20 2175 for each department” department” 30 1566.6667 1566.6667 1566.6667 Creating Groups of Data: GROUP BY Clause SELECT FROM [WHERE [GROUP BY [ORDER BY column, group_function(column) table condition] group_by_expression] column]; Divide rows in a table into smaller groups Divide by using the GROUP BY clause. by 5-13 Using the GROUP BY Clause All columns in the SELECT list that are not in group functions must be in the GROUP BY clause. BY SQL> SELECT deptno, AVG(sal) 2 FROM emp 3 GROUP BY deptno; DEPTNO AVG(SAL) --------- --------10 2916.6667 20 2175 30 1566.6667 5-14 Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list. be SQL> SELECT AVG(sal) 2 FROM emp 3 GROUP BY deptno; AVG(SAL) --------2916.6667 2175 1566.6667 5-15 EMP DEPTNO --------10 10 10 20 20 20 20 20 30 30 30 30 30 30 5-16 Grouping by More Than One Column JOB SAL --------- --------MANAGER 2450 PRESIDENT 5000 CLERK 1300 CLERK 800 CLERK 1100 ANALYST 3000 ANALYST 3000 MANAGER 2975 SALESMAN 1600 MANAGER 2850 SALESMAN 1250 CLERK 950 SALESMAN 1500 SALESMAN 1250 “sum salaries in sum the EMP table the for each job, for grouped by department” department” DEPTNO -------10 10 10 20 20 20 30 30 30 JOB SUM(SAL) --------- --------CLERK 1300 MANAGER 2450 PRESIDENT 5000 ANALYST 6000 CLERK 1900 MANAGER 2975 CLERK 950 MANAGER 2850 SALESMAN 5600 Using the GROUP BY Clause on Multiple Columns SQL> SELECT deptno, job, sum(sal) 2 FROM emp 3 GROUP BY deptno, job; DEPTNO JOB SUM(SAL) --------- --------- --------10 CLERK 1300 10 MANAGER 2450 10 PRESIDENT 5000 20 ANALYST 6000 20 CLERK 1900 ... 9 rows selected. 5-17 Illegal Queries Using Group Functions Any column or expression in the SELECT Any list that is not an aggregate function must se be in the GROUP BY clause. be lau SQL> SELECT 2 FROM deptno, COUNT(ename) UP emp; RO eG th in SELECT deptno, COUNT(ename) g in * iss ERROR at line 1: m ORA-00937: not ansingle-group group m lu Co 5-18 BY cl function Illegal Queries Using Group Functions • You cannot use the WHERE clause to restrict groups. • You use the HAVING clause to restrict groups. SQL> 2 3 4 SELECT FROM WHERE GROUP BY deptno, AVG(sal) emp AVG(sal) > 2000 deptno; se lau c RE s HE oup e W t gr WHERE AVG(sal) > 2000 th ic * se str t u re ERROR at line 3: no to is not allowed ORA-00934: group function an C 5-19 here Excluding Group Results EMP DEPTNO SAL --------- --------10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 5-20 5000 3000 2850 “maximum salary per department greater than $2900” DEPTNO MAX(SAL) --------- --------10 5000 20 3000 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. SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY 5-21 column, group_function table condition] group_by_expression] group_condition] column]; 5-22 • The Oracle Server performs the following steps when you use the HAVING clause: – Rows are grouped. – The group function is applied to the group. – The groups that match the criteria in the HAVING clause are displayed. • The HAVING clause can precede the GROUP BY clause, but it is recommended that you place the GROUP BY clause first because it is more logical. Groups are formed and group functions are calculated before the HAVING clause is applied to the groups in the SELECT list. Using the HAVING Clause This example displays department numbers and maximum salary for those departments whose maximum salary is greater than $2900. SQL> 2 3 4 SELECT FROM GROUP BY HAVING deptno, max(sal) emp deptno max(sal)>2900; DEPTNO MAX(SAL) --------- --------10 5000 20 3000 5-23 Using the HAVING Clause This example displays the job title and total monthly salary for each job title with a total payroll exceeding $5000. The example excludes salesmen and sorts the list by the total monthly salary. SQL> 2 3 4 5 6 SELECT FROM WHERE GROUP BY HAVING ORDER BY job, SUM(sal) PAYROLL emp job NOT LIKE 'SALES%' job SUM(sal)>5000 SUM(sal); JOB PAYROLL --------- --------ANALYST 6000 MANAGER 8275 5-24 Nesting Group Functions Display the maximum average salary. Display SQL> SELECT max(avg(sal)) 2 FROM emp 3 GROUP BY deptno; MAX(AVG(SAL)) ------------2916.6667 5-25 Summary SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY column, group_function (column) table condition] group_by_expression] group_condition] column]; Order of evaluation of the clauses: • WHERE clause • GROUP BY clause • HAVING clause 5-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 5-27 ...
View Full Document

This note was uploaded on 07/15/2011 for the course ECO 2023 taught by Professor Mr.raza during the Summer '10 term at FAU.

Page1 / 27

sql05 - 5 Aggregating Data Using Group Functions Objectives...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online