Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 38 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
38
Dung lượng
271,23 KB
Nội dung
Group Functions
5
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder5Ć2
Schedule: Timing Topic
35 minutes Lecture
40 minutes Practice
75 minutes Total
Class Management Note:
Files required for this lesson are:
Demonstration: l5count1.sql, l5count2.sql, l5error.sql, l5order1.sql,
l5order2.sql, l5title1.sql, l5title2.sql
Practice: None
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 AV G
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.
Class Management Note:
Stress the use of DISTINCT and group functions ignoring null values. ALL
is the default and is very rarely specified.
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
Class Management Note:
DEMO: l5count1.sql
PURPOSE: Demonstrate the use of COUNT(*) as shown on the next page.
Note that the total number of employees in department 31 is returned.
DEMO: l5count2.sql
PURPOSE: Compare the count of all employees versus only those who earn
commission. Notice that these numbers are different.
Ask the students what the value would be when comparing COUNT(ID)
and COUNT(*). The values would be the same because ID was created as a
NOT NULL column.
[...]... 1 Group Functions 5Ć11 5Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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 FROM [WHERE [GROUP BY [ORDER BY column, group_ function table condition] group_ by_expression] column]; where: group_ by_expression... SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY column, group_ function table condition] group_ by_expression] group_ condition] column]; where: group_ condition restricts the groups of rows returned to those groups for which the specified condition is TRUE The Oracle7 Server performs the following steps when you use the HAVING clause: D Rows are grouped D The group function is applied D The groups that match... SQL: D AVG D COUNT D MAX D MIN D STDDEV D SUM D VARIANCE Syntax SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY column, group_ function table condition] group_ by_expression] group_ condition] column]; You can create subgroups by using the GROUP BY clause Groups can be excluded using the HAVING clause Place the HAVING and GROUP BY clauses after the WHERE clause in a statement Place the ORDER BY clause last... 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 Class Management Note: Until now, all group functions have treated the table as one large group of information Using the GROUP BY clause, information about each department can be represented, rather than grouping all the results into one value Point out that the GROUP. .. 1: ORA-00937: not a single -group group function Correct the above error by adding the GROUP BY clause Now, REGION_ID is the name of a group SQL> SELECT region_id, COUNT(name) 2 FROM s_dept 3 GROUP BY region_id; REGION_ID COUNT(NAME) - 1 4 2 2 3 2 4 2 5 2 Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause Group Functions 5Ć19 5Ć20 Introduction... The server identifies the groups specified in the GROUP BY clause 3 The HAVING clause further restricts result groups that do not meet the group criteria in the HAVING clause Group Functions 5Ć31 5Ć32 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice Overview At the end of this practice, you should be familiar with using group functions and selecting groups of data Practice Contents... display the grouped rows again, but this time only show groups whose total salaries exceed 5000 Note that the “President” title is removed from the list Group Functions 5Ć27 5Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder The HAVING Clause continued In the following example, there is no group function in the SELECT list, but there are HAVING and GROUP BY clauses Since a group function... the GROUP BY clause is left off for the combination of regular column and group function in the SELECT clause, an error occurs To correct the error, demonstrate adding the GROUP BY clause ( GROUP BY region_id”) after the FROM clause 5Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Illegal Queries Using Group Functions Whenever you use a mixture of individual items (REGION_ID) and group. .. match the criteria in the HAVING clause are displayed The HAVING clause may 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 Group Functions 5Ć25 5Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder... title group, without displaying the title SQL> SELECT 2 FROM 3 GROUP BY MAX(salary) s_emp title; MAX(SALARY) 2500 1525 1400 8 rows selected Example Display the maximum salary for each title group and display the title SQL> SELECT 2 FROM 3 GROUP BY title, MAX(salary) s_emp title; TITLE MAX(SALARY) - President 2500 Sales Representative 1525 Stock Clerk 1400 8 rows selected Group . 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. 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