25
Aggregating Data Using Group Functions

Aggregating Data Using Group Functions

  • Upload
    nevina

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

Aggregating Data Using Group Functions. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Aggregating  Data Using  Group Functions

Aggregating Data Using Group Functions

Page 2: Aggregating  Data Using  Group Functions

Objectives

After completing this lesson, you should be able to do the following:

Identify the available group functionsDescribe the use of group functionsGroup data using the GROUP BY clauseInclude or exclude grouped rows by using the HAVING clause

Page 3: Aggregating  Data Using  Group Functions

What Are Group Functions?Group functions operate on sets of rows to give one result per group.

Employee

“maximum salary in

the Employee table”

dept_nbr salary--------- --------- 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

MAX(salary)

-----------

5000

Page 4: Aggregating  Data Using  Group Functions

Types of Group Functions

AVG COUNT MAXMIN STDDEVSUMVARIANCE

Page 5: Aggregating  Data Using  Group Functions

Using Group Functions

SELECT [column,] group_function(column)FROM table[WHERE condition][GROUP BY column][ORDER BY column];

Page 6: Aggregating  Data Using  Group Functions

Using AVG & SUM Functions

You can use AVG and SUM for numeric data.

AVG(salary) MAX(salary) MIN(salary) SUM(salary)----------- ---------- ----------- ----------- 1400 1600 1250 5600

MySQL>SELECT AVG(salary), MAX(salary), -> MIN(salary), SUM(salary) ->FROM employee ->WHERE job LIKE 'salaryes';

Page 7: Aggregating  Data Using  Group Functions

Using MIN & MAX Functions

You can use MIN and MAX for any datatype.

MySQL>SELECT MIN(hire_date), MAX(hire_date) ->FROM employee;

MIN(HIRED) MAX(HIRED)--------- ---------17-DEC-80 12-JAN-83

Page 8: Aggregating  Data Using  Group Functions

Using the COUNT Function

COUNT(*) returns the number of rows in a table.

COUNT(*)--------- 6

MySQL>SELECT COUNT(*) ->FROM employee ->WHERE dept_nbr = 30;

Page 9: Aggregating  Data Using  Group Functions

Using the COUNT Function

COUNT(expr) returns the number of nonnull rows.

MySQL>SELECT COUNT(commission) ->FROM employee ->WHERE dept_nbr = 30;

COUNT(Commission)----------------- 4

Page 10: Aggregating  Data Using  Group Functions

Group Functions & Null Values

Group functions ignore null values in the column.

MySQL>SELECT AVG(commission) ->FROM employee;

AVG(Commission)---------------- 550

Page 11: Aggregating  Data Using  Group Functions

Using the NVL Function withGroup Functions

The NVL function forces group functions to include null values.

MySQL>SELECT AVG(IFNULL(commission,0)) ->FROM employee;

Average_Commission------------------ 157.14286

Page 12: Aggregating  Data Using  Group Functions

Creating Groups of Data Employee

“averagesalary

in Employeetable

for each department”

2916.6667

2175

1566.6667

dept_nbr salary--------- -------- 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

dept_nbr AVG(salary)

-------- -----------

10 2916.6667

20 2175

30 1566.6667

Page 13: Aggregating  Data Using  Group Functions

Creating Groups of Data:GROUP BY Clause

Divide rows in a table into smaller groups by using the GROUP BY clause.

SELECT column, group_function(column)FROM table[WHERE condition][GROUP BY group_by_expression][ORDER BY column];

Page 14: Aggregating  Data Using  Group Functions

Using the GROUP BY Clause

All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.

MySQL>SELECT dept_nbr, AVG(salary) ->FROM employee ->GROUP BY dept_nbr;

dept_nbr AVG(salary)--------- --------- 10 2916.6667 20 2175 30 1566.6667

Page 15: Aggregating  Data Using  Group Functions

Using the GROUP BY Clause

The GROUP BY column does not have to be in the SELECT list.

MySQL>SELECT AVG(salary) ->FROM employee ->GROUP BY dept_nbr ;

AVG(salary)--------- 2916.6667 21751566.6667

Page 16: Aggregating  Data Using  Group Functions

Grouping by More Than One ColumnEmployee

“sum salaries in the Employeetable for eachjob, grouped

by department”

dept_nbr Job salary

--------- --------- ---------

10 Manager 2450

10 President 5000

10 Clerk 1300

20 Clerk 800

20 Clerk 1100

20 Analyst 3000

20 Analyst 3000

20 Manager 2975

30 Salesman 1600

30 Manager 2850

30 Salesman 1250

30 Clerk 950

30 Salesman 1500

30 Salesman 1250

Job SUM(salary)

--------- ---------

Clerk 1300

Manager 2450

President 5000

Analyst 6000

Clerk 1900

Manager 2975

Clerk 950

Manager 2850

Salesman 5600

dept_nbr

--------

10

10

10

20

20

20

30

30

30

Page 17: Aggregating  Data Using  Group Functions

Using the GROUP BY Clause on Multiple Columns

MySQL> SELECT dept_nbr, job, sum(salary) -> FROM employee -> GROUP BY dept_nbr, job;

dept_nbr job SUM(salary)--------- --------- ----------- 10 Clerk 1300 10 Manager 2450 10 President 5000 20 Analyst 6000 20 Clerk 1900...9 rows selected.

Page 18: Aggregating  Data Using  Group Functions

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.

MySQL> SELECT dept_nbr, COUNT(name) -> FROM employee;

dept_nbr count(name)-------- -----------

20 14

Column missing in the GROUP BY clause

Page 19: Aggregating  Data Using  Group Functions

Illegal Queries Using Group Functions

You cannot use the WHERE clause to restrict groups.You use the HAVING clause to restrict groups.

MySQL> SELECT dept_nbr, AVG(salary) -> FROM employee -> WHERE AVG(salary) > 2000 -> GROUP BY dept_nbr;

ERROR 111: Invalid use of group functions

Cannot use the WHERE clause

to restric

t groups

Page 20: Aggregating  Data Using  Group Functions

Excluding Group Results

“maximumsalary

per departmentgreater than

$2900”

EMP

5000

3000

2850

dept_nbr salary

--------- ---------

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

dept_nbr MAX(salary)

--------- ----------

10 5000

20 3000

Page 21: Aggregating  Data Using  Group Functions

Excluding Group Results: HAVING Clause

Use the HAVING clause to restrict groupsRows are grouped.The group function is applied.Groups matching the HAVING clause are displayed.

SELECT column, group_functionFROM table[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY column];

Page 22: Aggregating  Data Using  Group Functions

Using the HAVING Clause

MySQL> SELECT dept_nbr, max(salary) -> FROM employee -> GROUP BY dept_nbr -> HAVING max(salary)>2900;

dept_nbr MAX(salary)--------- ----------- 10 5000 20 3000

Page 23: Aggregating  Data Using  Group Functions

Using the HAVING Clause

MySQL> SELECT job, SUM(salary) PAYROLL -> FROM employee -> WHERE job NOT LIKE 'salaryies' -> GROUP BY job -> HAVING SUM(salary)>5000 -> ORDER BY SUM(salary);

JOB PAYROLL--------- ---------Analyst 6000Manager 8275

5 HAVING SUM(salary)>5000

Page 24: Aggregating  Data Using  Group Functions

Nesting Group Functions

Display the maximum average salary.

MySQL> SELECT max(avg(salary)) -> FROM employee -> GROUP BY dept_nbr;

MAX(AVG(salary))------------- 2916.6667

Page 25: Aggregating  Data Using  Group Functions

Summary

Order of evaluation of the clauses:WHERE clauseGROUP BY clauseHAVING clause

SELECT column, group_function(column)FROM table[WHERE condition][GROUP BY group_by_expression][HAVING group_condition][ORDER BY column];