group function

advertisement
Group Functions
Group Functions
•
Group functions operate on sets of rows to give
one result per group.
•
Group functions appear in both SELECT lists and
HAVING clauses.
•
The GROUP BY clause in the SELECT statement
divides rows into smaller groups.
•
The HAVING clause restricts result groups.
5-2
GROUP BY and HAVING Clauses in the
SELECT Statement: Syntax
SELECT
FROM
[WHERE
[GROUP BY
[HAVING
column, group_function
table
condition]
group_by_expression]
group_condition]
[ORDER BY column];
•
•
5-3
GROUP BY divides rows into smaller groups.
HAVING further restricts the result groups.
Group Functions
•
•
•
•
•
5-4
AVG (DISTINCT|ALL|)
COUNT (DISTINCT|ALL|expr|*)
MAX (DISTINCT|ALL|expr)
MIN (DISTINCT|ALL|expr)
SUM (DISTINCT|ALL|)
Group Functions: Example
• You can use AVG and SUM against columns that
can store numeric data.
SELECT
FROM
WHERE
•
You can use MAX and MIN for any datatype.
SELECT
FROM
5-5
AVG(salary), MAX(salary),
MIN(salary), SUM(salary)
s_emp
UPPER(title) LIKE 'SALES%';
MIN(last_name), MAX(last_name)
s_emp;
COUNT Function: Examples
•
COUNT(*) returns the number of rows in a table.
SELECT
FROM
WHERE
•
COUNT(expr) returns the number of non-null rows.
SELECT
FROM
WHERE
5-6
COUNT(*)
s_emp
dept_id = 31;
COUNT(commission_pct)
s_emp
dept_id = 31;
The GROUP BY Clause: Syntax
SELECT
FROM
column, group_function
table
[WHERE
[GROUP BY
[ORDER BY
condition]
group_by_expression]
column];
•
Divide rows in a table into smaller groups by using
the GROUP BY clause.
•
Include the column list in the GROUP BY clause if
columns are used in the SELECT clause.
•
Override the default sort order by using the
ORDER BY clause.
5-7
Without the GROUP BY Clause
SELECT
FROM
WHERE
ID
-2
6
16
17
5-8
id, last_name, dept_id DEPARTMENT
s_emp
dept_id = 41;
LAST_NAME DEPARTMENT
--------- ---------Ngao
41
Urguhart
41
Maduro
41
Smith
41
Department 41
displays four times
because it appears
as the department
number of four
employees.
With the GROUP BY Clause
SELECT
FROM
dept_id, COUNT(*) ”Number”
s_emp
WHERE
GROUP BY
dept_id = 41
dept_id;
DEPT_ID Number
------- -----41
4
5-9
The GROUP BY clause
displays one line of data
for each department
retrieved in the WHERE
clause, and COUNT(*)
displays the number of
employees in each
department (group)
displayed.
The GROUP BY Clause: Examples
•
Number of customers in each credit rating
SELECT
FROM
GROUP BY
credit_rating, COUNT(*)
s_customer
credit_rating;
• Job titles and monthly salary for each job title
5-10
SELECT
FROM
title, SUM(salary) PAYROLL
s_emp
WHERE
GROUP BY
ORDER BY
title NOT LIKE 'VP%'
title
SUM(salary);
The GROUP BY Clause: Example
•
All columns in the SELECT list that are not in
group functions must be in the GROUP BY
clause.
•
The GROUP BY column does not have to be in
the SELECT clause.
•
The results are more meaningful if the GROUP BY
column is in the SELECT clause.
SELECT
FROM
GROUP BY
5-11
title, MAX(salary)
s_emp
title;
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.
•
You will see an error message if you do not
correctly create your GROUP BY clause.
SELECT
FROM
region_id, COUNT(name)
s_dept;
SELECT region_id, COUNT(name)
*
ERROR at line 1:
ORA-00937: not a single-group group function
5-12
Illegal Queries Using Group Functions
•
You cannot use the WHERE clause to restrict
groups.
•
Use the HAVING clause to restrict groups.
SELECT
FROM
WHERE
GROUP BY
dept_id, AVG(salary)
s_emp
AVG(salary) > 2000
dept_id;
WHERE AVG(salary) > 2000
*
ERROR at line 3:
ORA-00934: group function is not allowed here
5-13
Groups Within Groups: Example
•
Return summary results for groups and
subgroups by listing more than one GROUP BY
column.
•
Determine the default sort order of the results by
the order of the columns in the GROUP BY clause.
SELECT
FROM
GROUP BY
5-14
dept_id, title, COUNT(*)
s_emp
dept_id, title;
Display Specific Rows
by Using the WHERE Clause
SELECT
FROM
WHERE
LAST_NAME
--------Velasquez
5-15
last_name, title
s_emp
last_name LIKE ’V%’;
TITLE
---------President
WHERE clause
(Restrict
Rows)
Display a
specific
employee as
restricted in
the WHERE
clause
Display Specific Groups by
Using the HAVING Clause
SELECT
FROM
GROUP BY
HAVING
title, 12 * AVG(salary) SALARY,
COUNT(*)
NUMBEROFEMPLOYEES
s_emp
title
COUNT(*) > 2;
HAVING clause (Restrict Groups)
TITLE
SALARY
NUMBEROFEMPLOYEES
-------------------- -------------- ------------------Sales Representative
$17,712.00
5
Stock Clerk
$11,388.00
10
Warehouse Manager
$14,776.80
5
Display specific groups of job titles as
restricted in the HAVING clause.
5-16
The HAVING Clause: Syntax
SELECT
FROM
[WHERE
[GROUP BY
column, group_function
table
condition]
group_by_expression]
[HAVING
[ORDER BY
group_condition]
column];
Use the HAVING clause to further restrict groups.
5-17
–
–
Step 1: Rows are grouped.
–
Step 3: Groups matching the HAVING
condition are displayed.
Step 2: The group function is applied to the
group.
The HAVING Clause: Example
The “President” group does not appear in the output
because it does not meet the criteria.
SELECT
FROM
title, SUM(salary) PAYROLL
s_emp
WHERE
GROUP BY
HAVING
ORDER BY
title NOT LIKE 'VP%'
title
SUM(salary) > 5000
SUM(salary);
5-18
The HAVING Clause: Example
•
You can use the GROUP BY clause without using a
group function in the SELECT list.
•
If you restrict rows based on the result of a group
function, then you must have a GROUP BY clause
as well as the HAVING clause.
SELECT
FROM
GROUP BY
dept_id
s_emp
dept_id
HAVING
SUM(salary) > 4000;
5-19
Download