1. Trang chủ
  2. » Công Nghệ Thông Tin

OCA /OCP Oracle Database 11g A ll-in-One Exam Guide- P52 pot

10 106 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 401,27 KB

Nội dung

OCA/OCP Oracle Database 11g All-in-One Exam Guide 466 TIP Grouping data and using summary functions are widely utilized for reporting purposes. It is valuable to practice the segmentation of a set of data into different groups. Oracle provides the analytical language to deconstruct datasets into groups, divide these into further subgroups, and so on. Aggregate grouping functions can then be executed against these groups and subgroups. The GROUP BY Clause The SELECT statement is enhanced by the addition of the GROUP BY clause. This clause facilitates the creation of groups. It appears after the WHERE clause but before the ORDER BY clause, as follows: Figure 11-2 Unique DEPARTMENT_ID values in the EMPLOYEES table Chapter 11: Group Functions 467 PART II SELECT column|expression|group_function(column|expression [alias]), } FROM table [WHERE condition(s)] [GROUP BY {col(s)|expr}] [ORDER BY {col(s)|expr|numeric_pos} [ASC|DESC] [NULLS FIRST|LAST]]; The column or expression specified in the GROUP BY clause is also known as the grouping attribute and is the component that rows are grouped by. The dataset is segmented according to the grouping attribute. Consider the following query: select max(salary), count(*) from employees group by department_id order by department_id; The grouping attribute in this example is the DEPARTMENT_ID column. The dataset, on which the group functions in the SELECT list must operate, is divided into 12 groups, one for each department. For each group (department), the maximum salary value and the number of rows are returned. Since the results are sorted by DEPARTMENT_ID, the third row in the set of results contains the values 11000 and 6. This indicates that 6 employees have a DEPARTMENT_ID value of 30. Of these 6, the highest earner has a SALARY value of 11000. This query demonstrates that the grouping attribute does not have to be included in the SELECT list. It is common to see the grouping attribute in the SELECT list alongside grouping functions. If an item that is not a group function appears in the SELECT list and there is no GROUP BY clause, an “ORA-00937: not a single-group group function” error is raised. If a GROUP BY clause is present but that item is not a grouping attribute, then an “ORA-00979: not a GROUP BY expression” error is returned. EXAM TIP Any item in the SELECT list that is not a group function must be a grouping attribute of the GROUP BY clause. If a group function is placed in a WHERE clause, an “ORA-00934: group function is not allowed here” error is returned. Imposing group-level conditions is achieved using the HAVING clause discussed later in this chapter. Group functions may, however, be used as part of the ORDER BY clause. The first query in Figure 11-3 raises an error because the END_DATE column is in the SELECT list with a group function and there is no GROUP BY clause. An ORA-00979 error is returned from the second query, since the START_DATE item is listed in the SELECT clause, but it is not a grouping attribute. The third query divides the JOB_HISTORY rows into groups based on the four-digit year component from the END_DATE column. Four groups are created using this grouping attribute. These represent different years when employees ended their jobs. The COUNT shows the number of employees who quit their jobs during each of these years. The results are listed in descending order based on the “Number of Employees” expression. Note that the COUNT group function is present in the ORDER BY clause. OCA/OCP Oracle Database 11g All-in-One Exam Guide 468 EXAM TIP A dataset is divided into groups using the GROUP BY clause. The grouping attribute is the common key shared by members of each group. The grouping attribute is usually a single column but may be multiple columns or an expression that cannot be based on group functions. Note that only grouping attributes and group functions are permitted in the SELECT clause when using GROUP BY. Grouping by Multiple Columns A powerful extension to the GROUP BY clause uses multiple grouping attributes. Oracle permits datasets to be partitioned into groups and allows these groups to be further divided into subgroups using a different grouping attribute. Consider the following two queries: Query 1: select department_id, sum(commission_pct) from employees where commission_pct is not null group by department_id; Query 2: select department_id, job_id, sum(commission_pct) from employees where commission_pct is not null group by department_id, job_id; Query 1 restricts the rows returned from the EMPLOYEES table to the 35 rows with non-null COMMISSION_PCT values. These rows are then divided into two Figure 11-3 The GROUP BY clause Chapter 11: Group Functions 469 PART II groups: 80 and NULL based on the DEPARTMENT_ID grouping attribute. The result set contains two rows, which return the sum of the COMMISSION_PCT values for each group. Query 2 is similar to the first one except it has an additional item: JOB_ID in both the SELECT and GROUP BY clauses. This second grouping attribute decomposes the two groups by DEPARTMENT_ID into the constituent JOB_ID components belonging to the rows in each group. The distinct JOB_ID values for rows with DEPARTMENT_ ID=80 are SA_REP and SA_MAN. The distinct JOB_ID value for rows with null DEPARTMENT_ID is SA_REP. Therefore, query 2 returns two groupings, one that consists of two subgroups, and the other with only one, as shown in Figure 11-4. Exercise 11-2: Group Data Based on Multiple Columns Analysis of staff turnover is a common reporting requirement. You are required to create a report that contains the number of employees who left their jobs, grouped by the year in which they left. The jobs they performed is also required. The results must be sorted in descending order based on the number of employees in each group. The report must list the year, the JOB_ID, and the number of employees who left a particular job in that year. 1. Start SQL Developer and connect to the HR schema. 2. The JOB_HISTORY table contains the END_DATE and JOB_ID columns, which constitute the source data for this report. 3. The year component may be extracted using the TO_CHAR function. The number of employees who quit a particular job in each year may be obtained using the COUNT(*) function. Figure 11-4 The GROUP BY clause with multiple columns OCA/OCP Oracle Database 11g All-in-One Exam Guide 470 4. Executing the following statement returns the staff turnover report as requested: select to_char(end_date,'yyyy') "Quitting Year" ,job_id, count(*) "Number of Employees" from job_history group by to_char(end_date,'yyyy'), job_id order by count(*) desc; Nested Group Functions Recall that single-row functions may be nested or embedded to any level of depth. Group functions may only be nested two levels deep. Three formats using group functions are shown here: G1(group_item) = result G1(G2(group_item ) = result G1(G2(G3(group_item))) is NOT allowed. Group functions are represented by the letter G followed by a number. The first simple form contains no nested functions. Examples include the SUM(group_item) or AVG(group_item) functions that return a single result per group. The second form supports two nested group functions, like SUM(AVG(group_item)). In this case, a GROUP BY clause is necessary because the average value of the group_item per group is calculated before being aggregated by the SUM function. The third form is disallowed by Oracle. Consider an expression that nests three group functions. If the MAX function is applied to the previous example, the expression MAX(SUM(AVG(group_item))) is formed. The two inner group functions return a single value representing the sum of a set of average values. This expression becomes MAX(single value), which is not sensible, since a group function cannot be applied to a single value. Figure 11-5 demonstrates two queries. Both restrict the rows returned to those with DEPARTMENT_ID values of null, 40, and 80. These are then partitioned by their DEPARTMENT_ID values into three groups. The first query calculates the sum of the COMMISSION_PCT values for each group and returns the values 0.15, null, and 7.65. Query 2 contains the nested group functions, which may be evaluated as follows: AVG(SUM(COMMISSION_PCT)) = (0.15 + 7.65) /2 = 3.9. EXAM TIP Single-row functions may be nested to any level, but group functions may be nested to at most two levels deep. The nested function call COUNT(SUM(AVG( X))) returns the error “ORA-00935: group function is nested too deeply.” It is acceptable to nest single-row functions within group functions. Consider the following query: SELECT SUM(AVG(LENGTH(LAST_ NAME))) FROM EMPLOYEES GROUP BY DEPARTMENT_ID. It calculates the sum of the average length of LAST_NAME values per department. Chapter 11: Group Functions 471 PART II Include or Exclude Grouped Rows Using the HAVING Clause Creating groups of data and applying aggregate functions is very useful. A refinement to these features is the ability to include or exclude results based on group-level conditions. This section introduces the HAVING clause. A clear distinction is made between the WHERE clause and the HAVING clause. Figure 11-5 Nested group functions OCA/OCP Oracle Database 11g All-in-One Exam Guide 472 Restricting Group Results WHERE clause conditions restrict rows returned by a query. Rows are included if they fulfill the conditions listed and are sometimes known as row-level results. Clustering rows using the GROUP BY clause and applying an aggregate function to these groups returns results often referred to as group-level results. The HAVING clause restricts group-level results. The following query limits the rows retrieved from the JOB_HISTORY table by specifying a WHERE condition based on the DEPARTMENT_ID column values. select department_id from job_history where department_id in (50,60,80,110); This query returns seven rows. If the WHERE clause were absent, all ten rows would be retrieved. Suppose you want to know how many employees were previously employed in each of these departments. There are seven rows that can be manually grouped and counted. However, if there are a large number of rows, an aggregate function like COUNT may be used, as shown in the following query: select department_id, count(*) from job_history where department_id in (50,60,80,110) group by department_id; This query adds to the previous statement. The aggregate function COUNT was added to the SELECT list, and a GROUP BY DEPARTMENT_ID clause was also added. Four rows with their aggregate row count are returned, and it is clear that the original seven rows restricted by the WHERE clause were clustered into four groups based on common DEPARTMENT_ID values, as shown in the following table: DEPARTMENT_ID COUNT(*) 50 2 60 1 80 2 110 2 Suppose you wanted to restrict this list to only those departments with more than one employee. The HAVING clause limits or restricts the group-level rows as required. This query must perform the following steps: 1. Consider the entire row-level dataset. 2. Limit the dataset by any WHERE clause conditions. 3. Segment the data into one or more groups using the grouping attributes specified in the GROUP BY clause. 4. Apply any aggregate functions to create a new group-level dataset. Each row may be regarded as an aggregation of its source row-level data based on the groups created. Chapter 11: Group Functions 473 PART II 5. Limit or restrict the group-level data with a HAVING clause condition. Only group-level results matching these conditions are returned. TIP Choosing the appropriate context to use a WHERE or a HAVING clause depends on whether actual rows or group-level rows are to be restricted. When actual (physical) rows are restricted, one or more conditions are imposed using a WHERE clause. When these rows are grouped together, one or more aggregate functions may be applied, yielding one or more group-level rows that may be restricted using a HAVING clause. The HAVING Clause The general form of the SELECT statement is further enhanced by the addition of the HAVING clause and becomes SELECT column|expression|group_function(column|expression [alias]), } FROM table [WHERE condition(s)] [GROUP BY {col(s)|expr}] [HAVING group_condition(s)] [ORDER BY {col(s)|expr|numeric_pos} [ASC|DESC] [NULLS FIRST|LAST]]; An important difference between the HAVING clause and the other SELECT statement clauses is that it may only be specified if a GROUP BY clause is present. This dependency is sensible, since group-level rows must exist before they can be restricted. The HAVING clause can occur before the GROUP BY clause in the SELECT statement. However, it is more common to place the HAVING clause after the GROUP BY clause. All grouping is performed and group functions are executed prior to evaluating the HAVING clause. The following query shows how the HAVING clause is used to restrict an aggregated dataset. Records from the JOB_HISTORY table are divided into four groups. The rows that meet the HAVING clause condition (contributing more than one row to the group row count) are returned: select department_id, count(*) from job_history where department_id in (50,60,80,110) group by department_id having count(*)>1 Three rows with DEPARTMENT_ID values of 50, 80, and 110, each with a COUNT(*) value of 2, are returned. Figure 11-6 shows three queries. Query 1 divides the 107 records from the EMPLOYEES table into 19 groups based on common JOB_ID values. The average salary for each JOB_ID group and the aggregate row count are computed. Query 2 refines the results by conditionally excluding those aggregated rows where the average salary is less than or equal to 10000, using a HAVING clause. Query 3 demonstrates that the Boolean operators may be used to specify multiple HAVING clause conditions. OCA/OCP Oracle Database 11g All-in-One Exam Guide 474 EXAM TIP The HAVING clause may only be specified when a GROUP BY clause is present. A GROUP BY clause can be specified without a HAVING clause. Exercise 11-3: Use the HAVING Clause The company is planning a recruitment drive and wants to identify which days of the week 20 or more staff members were hired. Your report must list the days and the number of employees hired on each of them. 1. Start SQL*Plus or SQL Developer and connect to the HR schema. 2. Divide EMPLOYEES records into groups based on the day component of the HIRE_DATE column. You can obtain the number of employees per group Figure 11-6 The HAVING clause Chapter 11: Group Functions 475 PART II using the COUNT function. Use the HAVING clause to restrict these rows to only those where the count is greater than or equal to 20. 3. A possible solution is the following statement, which returns the days of the week on which 20 or more employees were hired: select to_char(hire_date,'Day') hire_day, count(*) from employees group by to_char(hire_date,'Day') having count(*)>=20; Two-Minute Drill Describe the Group Functions • Group functions are also known as multiple-row, aggregate, or summary functions. They execute once for each group of data and aggregate the data from multiple rows into a single result for each group. • Groups may be entire tables or portions of a table grouped together by a common grouping attribute. Identify the Available Group Functions • The COUNT of a column or an expression returns an integer value representing the number of rows in a group, where the specified column or expression is not null. • The SUM function returns an aggregated total of all the non-null numeric values in a group. • The AVG function divides the sum of a column or expression by the number of non-null rows in a group. • The MAX and MIN functions operate on NUMBER, DATE, CHAR, and VARCHAR2 data types. They return a value that is either the largest or smallest item in the group. • Group functions may be nested at most two levels deep. Group Data Using the GROUP BY Clause • The GROUP BY clause specifies the grouping attribute rows must have in common for them to be clustered together. • The GROUP BY clause facilitates the creation of groups within a selected set of data and appears after the WHERE clause but before the ORDER BY clause. • Any item on the SELECT list that is not a group function must be a grouping attribute. • Group functions may not be placed in a WHERE clause. • Datasets may be partitioned into groups and further divided into subgroups based on multiple grouping attributes. . conditions. OCA/ OCP Oracle Database 11g All-in-One Exam Guide 474 EXAM TIP The HAVING clause may only be specified when a GROUP BY clause is present. A GROUP BY clause can be specified without a HAVING. OCA/ OCP Oracle Database 11g All-in-One Exam Guide 466 TIP Grouping data and using summary functions are widely utilized for reporting purposes. It is valuable to practice the segmentation. employees who quit a particular job in each year may be obtained using the COUNT(*) function. Figure 11-4 The GROUP BY clause with multiple columns OCA/ OCP Oracle Database 11g All-in-One Exam Guide 470

Ngày đăng: 06/07/2014, 13:20

TỪ KHÓA LIÊN QUAN