Tải bản đầy đủ (.pdf) (38 trang)

Tài liệu group functions ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (248.51 KB, 38 trang )

Group Functions
5
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć2
Group Functions 5Ć3
Objectives
This lesson further addresses functions. You will focus on obtaining summary
information, such as averages, for groups of rows. You will discuss how to group
rows in a table into smaller sets, and how to specify search criteria for groups of
rows.
At the end of this lesson, you should be able to
D
Identify the available group functions.
D
Explain the use of group functions.
D
Use the GROUP BY clause to force statistics to be displayed for different groups.
D
Use the HAVING clause to include or exclude grouped rows.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć4
Group Functions 5Ć5
Overview
Unlike single row functions, group functions operate on sets of rows to give one
result per group. These sets may be the whole table or the table split into groups.
Group functions appear in both SELECT lists and HAVING clauses.
Group Functions
D
AVG
D
COUNT
D
MAX


D
MIN
D
STDDEV
D
SUM
D
VARIANCE
GROUP BY and HAVING Clauses in the SELECT Statement
By default, all the rows in a table are treated as one group. Use the GROUP BY
clause in the SELECT statement to divide rows into smaller groups. Additionally, to
restrict the result groups returned, use the HAVING clause.
Syntax
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
where: group_by_expression specifies columns whose values determine the
basis for grouping rows.
group_condition restricts the groups of rows returned to those
groups for which the specified condition is
TRUE.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć6
Group Functions 5Ć7
Group Functions
Each of the functions accepts an argument. The following table identifies the options
you can use in the syntax.
Function

Description
AVG(DISTINCT|ALL|n) Average value of n, ignoring null values.
COUNT(DISTINCT|ALL|expr|*) Number of rows, where expr evaluates to
something other than null. Count all selected
rows using *, including duplicates and rows
with nulls.
MAX(DISTINCT|ALL|expr) Maximum value of expr.
MIN(DISTINCT|ALL|expr) Minimum value of expr.
STDDEV(DISTINCT|ALL|n) Standard deviation of n, ignoring null values.
SUM(DISTINCT|ALL|n) Sum values of n, ignoring null values.
VARIANCE(DISTINCT|ALL|n) Variance of n, ignoring null values.
Guidelines
D
DISTINCT makes the function consider only non-duplicate values; ALL makes it
consider every value including duplicates. The default is ALL and therefore does
not need to be specified.
D
The datatypes for the arguments may be CHAR, VARCHAR2, NUMBER, or
DATE where expr is listed.
D
All group functions except COUNT(*) ignore null values. To substitute a value
for null values, use the NVL function.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć8
Group Functions 5Ć9
Group Functions
continued
Example
Display the average, highest, lowest, and sum of the monthly salaries for all sales
representatives.
SQL> SELECT AVG(salary), MAX(salary), MIN(salary),

2 SUM(salary)
3 FROM s_emp
4 WHERE UPPER(title) LIKE ’SALES%’;
AVG(SALARY) MAX(SALARY) MIN(SALARY) SUM(SALARY)
----------- ----------- ----------- -----------
1476 1525 1400 7380
Note: You can use AVG and SUM functions against columns that store numeric
data.
Example
Display the employee last name that is the first and the employee last name that is the
last in an alphabetized list of all employees.
SQL> SELECT MIN(last_name), MAX(last_name)
2 FROM s_emp;
MIN(LAST_NAME) MAX(LAST_NAME)
------------------------- -------------------------
Biri Velasquez
Note: You can use MAX and MIN functions for any datatype.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć10
Group Functions 5Ć11
Group Functions
continued
COUNT Function
The COUNT function has two formats: COUNT(*) and COUNT(expr). COUNT(*)
returns the number of rows in a table, including duplicate rows and rows containing
null values.
Example
Display the number of employees in department 31.
SQL> SELECT COUNT(*)
2 FROM s_emp
3 WHERE dept_id = 31;

COUNT(*)
----------
2
In contrast, COUNT(expr) returns the number of non-null rows in the column
identified by expr.
Example
Display the number of employees in department 31 who can earn a commission.
SQL> SELECT COUNT(commission_pct)
2 FROM s_emp
3 WHERE dept_id = 31;
COUNT(COMMISSION_PCT)
---------------------
1
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć12
Group Functions 5Ć13
The GROUP BY Clause
You use the GROUP BY clause to divide the rows in a table into smaller groups. You
can then use the group functions to return summary information for each group.
Syntax
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];
where: group_by_expression specifies columns whose values determine the
basis for grouping rows.
Guidelines
D
If you include a group function in a SELECT clause, you cannot select individual
results as well unless the individual column appears in the GROUP BY clause.

You will receive an error message if you fail to include the column list.
D
Using a WHERE clause, you can pre-exclude rows before dividing them into
groups.
D
You must include the columns in the GROUP BY clause.
D
You cannot use the positional notation or column alias in the GROUP BY clause.
D
By default, rows are sorted by ascending order of the GROUP BY list. You can
override this by using the ORDER BY clause.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć14
Group Functions 5Ć15
The GROUP BY Clause
continued
Example
Display each possible customer credit rating and the number of customers in each
credit rating category. Label the column # Cust.
SQL> SELECT credit_rating, COUNT(*) ”# Cust”
2 FROM s_customer
3 GROUP BY credit_rating;
CREDIT_RA # Cust
--------- ----------
EXCELLENT 9
GOOD 3
POOR 3
Example
Display the job titles and total monthly salary for each job title, excluding vice
presidents. Sort the list by the total monthly salary.
SQL> SELECT title, SUM(salary) PAYROLL

2 FROM s_emp
3 WHERE title NOT LIKE ’VP%’
4 GROUP BY title
5 ORDER BY SUM(salary);
TITLE PAYROLL
--------------------- ----------
President 2500
Warehouse Manager 6157
Sales Representative 7380
Stock Clerk 9490

×