Tài liệu group functions ppt

38 274 0
Tài liệu group functions ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 [...]... 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... Builder Summary There are seven group functions available in 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... all group functions, with the exception of STDDEV and VARIANCE D Grouping by rows to achieve more than one result D Excluding groups by using the HAVING clause Note: Column aliases used for the queries Group Functions 5Ć33 Practice 5 1 Determine the validity of the following statements Circle either True or False a Group functions work across many rows to produce one result True / False b Group functions. .. 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... 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... 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... clause to limit those rows SQL> 2 3 4 SELECT FROM GROUP BY HAVING dept_id, AVG(salary) s_emp dept_id AVG(salary) > 2000; DEPT_ID AVG(SALARY) - 50 2025 Group Functions 5Ć21 5Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Groups Within Groups You can return summary results for groups and subgroups by listing more than one GROUP BY column In the example, you count the number... Using Procedure Builder Illegal Queries Using Group Functions Whenever you use a mixture of individual items (REGION_ID) and group functions (COUNT) in the same SELECT statement, you must include a GROUP BY clause that specifies the individual items (in this case, REGION_ID) If the GROUP BY clause is missing, then the error message “not a single -group group function” would appear and an asterisk (*)... in the SELECT list, but there are HAVING and GROUP BY clauses Since a group function is referenced in the HAVING clause, the GROUP BY clause is required Example Display all department numbers for departments with a total monthly payroll more than 4000 SQL> 2 3 4 SELECT FROM GROUP BY HAVING dept_id s_emp dept_id SUM(salary) > 4000; DEPT_ID 41 50 Group Functions 5Ć29 5Ć30 Introduction to Oracle: SQL . 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. 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

Ngày đăng: 21/12/2013, 06:17

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan