advanced sql Functions in Oracle 10G phần 3 pot

42 352 0
advanced sql Functions in Oracle 10G phần 3 pot

Đ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

A SELECT with Just a FROMA SELECT with Just a FROM Clause SELECT empno, ename, orig_salary FROM employee Gives: EMPNO ENAME ORIG_SALARY 101 John 35000 102 Stephanie 35000 104 Christina 43000 108 David 37000 111 Katie 45000 106 Chloe 33000 122 Lindsey 40000 A SELECT with OrderingA SELECT with Ordering Note that the ordering is applied to the result set after the result is established: SELECT empno, ename, orig_salary FROM employee ORDER BY orig_salary Gives: EMPNO ENAME ORIG_SALARY 106 Chloe 33000 101 John 35000 102 Stephanie 35000 108 David 37000 122 Lindsey 40000 104 Christina 43000 111 Katie 45000 66 The Analytical Functions in Oracle (Analytical Functions I) A WHERE Clause Is Added to theA WHERE Clause Is Added to the Statement Notice that the WHERE has excluded rows before the final ordering: SELECT empno, ename, orig_salary FROM employee WHERE orig_salary < 43000 ORDER BY orig_salary Gives: EMPNO ENAME ORIG_SALARY 106 Chloe 33000 101 John 35000 102 Stephanie 35000 108 David 37000 122 Lindsey 40000 Notice that ORDER BY is applied last — after the SELECT FROM WHERE. An Analytical Function Is AddedAn Analytical Function Is Added to the Statementto the Statement Note here that the WHERE is applied before the RANK(). SELECT empno, ename, orig_salary, RANK() OVER(ORDER BY orig_salary) rankorder FROM employee WHERE orig_salary < 43000 ORDER BY orig_salary 67 Chapter | 3 Gives: EMPNO ENAME ORIG_SALARY RANKORDER 106 Chloe 33000 1 101 John 35000 2 102 Stephanie 35000 2 108 David 37000 4 122 Lindsey 40000 5 A Join Is Added to theA Join Is Added to the Statement What will happen to the order of execution if a join is included in the statement? We will add another table to the statement, then perform a join and see what hap- pens. Suppose we have a table called Job with this description: Name Null? Type EMPNO NUMBER(3) JOBTITLE VARCHAR2(20) and this data: EMPNO JOBTITLE 101 Chemist 102 Accountant 102 Mediator 111 Musician 122 Director Personnel 122 Mediator 108 Mediator 106 Computer Programmer 104 Head Mediator 68 The Analytical Functions in Oracle (Analytical Functions I) Now, we’ll perform a join with and without the analyti - cal function. The Join Without the AnalyticalThe Join Without the Analytical Function Just adding the join to the query shows that the join is performed with the other WHERE conditions: SELECT e.empno, e.ename, j.jobtitle, e.orig_salary FROM employee e, job j WHERE e.orig_salary < 43000 AND e.empno = j.empno Gives: EMPNO ENAME JOBTITLE ORIG_SALARY 101 John Chemist 35000 102 Stephanie Accountant 35000 102 Stephanie Mediator 35000 106 Chloe Computer Programmer 33000 108 David Mediator 37000 122 Lindsey Director Personnel 40000 122 Lindsey Mediator 40000 Here, the WHERE is used to filter all salaries that are less than 43000 and, because we are using a join (actu - ally an equi-join), the WHERE provides the equality condition for the equi-join. 69 Chapter | 3 Adding Ordering to a Joined ResultAdding Ordering to a Joined Result If an ordering is applied to the statement at this point, it occurs after the WHERE has been executed: SELECT e.empno, e.ename, j.jobtitle, e.orig_salary FROM employee e, job j WHERE e.orig_salary < 43000 AND e.empno = j.empno ORDER BY orig_salary desc Gives: EMPNO ENAME JOBTITLE ORIG_SALARY 122 Lindsey Director Personnel 40000 122 Lindsey Mediator 40000 108 David Mediator 37000 101 John Chemist 35000 102 Stephanie Accountant 35000 102 Stephanie Mediator 35000 106 Chloe Computer Programmer 33000 Note that the same number and content of rows is in the result set, and the ordering was applied after the WHERE clause. 70 The Analytical Functions in Oracle (Analytical Functions I) Adding an Analytical Function to aAdding an Analytical Function to a Query that Contains a Join (andQuery that Contains a Join (and Other WHERE Conditions)Other WHERE Conditions) In this query, we add the analytical function to the pre - vious statement to see where the analytical function is performed relative to the WHERE. SELECT e.empno, e.ename, j.jobtitle, e.orig_salary, RANK() OVER(ORDER BY e.orig_salary desc) rankorder FROM employee e, job j WHERE e.orig_salary < 43000 AND e.empno = j.empno ORDER BY orig_salary desc Gives: EMPNO ENAME JOBTITLE ORIG_SALARY RANKORDER 122 Lindsey Director Personnel 40000 1 122 Lindsey Mediator 40000 1 108 David Mediator 37000 3 101 John Chemist 35000 4 102 Stephanie Accountant 35000 4 102 Stephanie Mediator 35000 4 106 Chloe Computer Programmer 33000 7 Again, note that the joining (WHERE) preceded the use of the analytical function RANK. The RANK and ORDER BY are done together — last. 71 Chapter | 3 The Order with GROUP BY IsThe Order with GROUP BY Is Present Now, suppose we used a GROUP BY in a query with no ordering or analytical function: SELECT j.jobtitle, COUNT(*), MAX(orig_salary) maxsalary, MIN(orig_salary) minsalary FROM employee e, job j WHERE e.orig_salary < 43000 AND e.empno = j.empno GROUP BY j.jobtitle Gives: JOBTITLE COUNT(*) MAXSALARY MINSALARY Accountant 1 35000 35000 Chemist 1 35000 35000 Computer Programmer 1 33000 33000 Director Personnel 1 40000 40000 Mediator 3 40000 35000 Here we see the effect of the WHERE clause being applied before the GROUP BY. 72 The Analytical Functions in Oracle (Analytical Functions I) Adding Ordering to the QueryAdding Ordering to the Query Containing the GROUP BYContaining the GROUP BY This query can be reordered by the maximum original salary by adding an ORDER BY, which will keep the same number of rows but change the order of the dis - play. Here is the statement: SELECT j.jobtitle, COUNT(*), MAX(orig_salary) maxsalary, MIN(orig_salary) minsalary FROM employee e, job j WHERE e.orig_salary < 43000 AND e.empno = j.empno GROUP BY j.jobtitle ORDER BY maxsalary Which gives: JOBTITLE COUNT(*) MAXSALARY MINSALARY Computer Programmer 1 33000 33000 Accountant 1 35000 35000 Chemist 1 35000 35000 Director Personnel 1 40000 40000 Mediator 3 40000 35000 The ORDER BY is applied last. 73 Chapter | 3 Adding an Analytical Function toAdding an Analytical Function to the GROUP BY with ORDER BYthe GROUP BY with ORDER BY Version Notice that when the analytical function RANK is added to the statement, the RANK function is applied last, just before the ordering: SELECT j.jobtitle, COUNT(*), MAX(orig_salary) maxsalary, MIN(orig_salary) minsalary, RANK() OVER(ORDER BY MAX(orig_salary)) rankorder FROM employee e, job j WHERE e.orig_salary < 43000 AND e.empno = j.empno GROUP BY j.jobtitle ORDER BY rankorder Gives: JOBTITLE COUNT(*) MAXSALARY MINSALARY RANKORDER Computer Programmer 1 33000 33000 1 Accountant 1 35000 35000 2 Chemist 1 35000 35000 2 Director Personnel 1 40000 40000 4 Mediator 3 40000 35000 4 The final ORDER BY is redundant to the ordering in the RANK function in this case. However, as we pointed out earlier, the use of the final ORDER BY is the preferred way to use the functions. The ranking and ordering is done last. 74 The Analytical Functions in Oracle (Analytical Functions I) Changing the Final OrderingChanging the Final Ordering after Having Added anafter Having Added an Analytical FunctionAnalytical Function The final ORDER BY can rearrange the order of the display, hence showing the place of the RANK function is between the GROUP BY and the ORDER BY: SELECT j.jobtitle, COUNT(*), MAX(orig_salary) maxsalary, MIN(orig_salary) minsalary, RANK() OVER(ORDER BY MAX(orig_salary)) rankorder FROM employee e, job j WHERE e.orig_salary < 43000 AND e.empno = j.empno GROUP BY j.jobtitle ORDER BY j.jobtitle desc Gives: JOBTITLE COUNT(*) MAXSALARY MINSALARY RANKORDER Mediator 3 40000 35000 4 Director Personnel 1 40000 40000 4 Computer Programmer 1 33000 33000 1 Chemist 1 35000 35000 2 Accountant 1 35000 35000 2 75 Chapter | 3 [...]... -John 35 000 2 1 2 Chloe 33 000 1 3 1 Christina 430 00 6 7 5 David 37 000 4 1 3 Katie 45000 7 5 6 Lindsey 40000 5 6 4 Stephanie 35 000 3 3 2 79 The Analytical Functions in Oracle (Analytical Functions I) RNUM in this case is the ordering of salaries (low to high) with ties ignored had there not been other criteria The RANK and DENSE_RANK functions return their expected results, but the final ordering is... e.orig_salary < 430 00 AND e.empno = j.empno GROUP BY j.jobtitle HAVING MAX(orig_salary) > 34 000 ORDER BY j.jobtitle desc 76 Chapter | 3 Giving: JOBTITLE COUNT(*) MAXSALARY MINSALARY RANKORDER - -Mediator 3 40000 35 000 3 Director Personnel 1 40000 40000 3 Chemist 1 35 000 35 000 1 Accountant 1 35 000 35 000 1 The execution order is then: SELECT, FROM, WHERE, GROUP BY, HAVING, the... and then the final ORDER BY Where the Analytical Functions Can be Used in a SQL Statement All of the examples we have seen thus far show the analytical function being used in the result set of the SQL statement Since later versions of Oracle s SQL allow us to use subqueries in the result set as well as in the FROM and WHERE clauses, one might expect that analytical functions could be used in these clauses... employee ORDER BY ename 78 Chapter | 3 Which gives: EMPNO -106 104 108 101 111 122 102 ENAME ORIG_SALARY TOPRANK_ORIG CURR_SALARY TOPRANK_CURR - - - -Chloe 33 000 7 44000 4 Christina 430 00 2 55000 1 David 37 000 4 39 000 6 John 35 000 5 39 000 6 Katie 45000 1 49000 3 Lindsey 40000 3 52000 2 Stephanie 35 000 5 44000 4 Note that Katie has the highest original salary and hence her rank... 430 00 AND e.empno = j.empno GROUP BY j.jobtitle HAVING MAX(orig_salary) > 34 000 ORDER BY j.jobtitle desc Giving: JOBTITLE COUNT(*) MAXSALARY MINSALARY -Mediator 3 40000 35 000 Director Personnel 1 40000 40000 Chemist 1 35 000 35 000 Accountant 1 35 000 35 000 Then, with the RANK in place we get this: SELECT j.jobtitle, COUNT(*), MAX(orig_salary) maxsalary, MIN(orig_salary) minsalary,... 38 000 ORDER BY ename 85 The Analytical Functions in Oracle (Analytical Functions I) Gives: EMPNO -104 111 122 ENAME ORIG_SALARY CURR_SALARY RANK D_RANK - - - -Christina 430 00 55000 2 3 Katie 45000 49000 3 1 Lindsey 40000 52000 1 2 Execution Plan -0 SELECT STATEMENT Optimizer=CHOOSE 1 0 SORT (ORDER BY) 2 1 WINDOW (SORT) 3 2 WINDOW (SORT) 4 3. .. the rows anywhere in any order Oracle does allow duplicate rows, but defining an appropriate primary key would prevent this We will not pursue this issue at this time, but the point is that some data is loaded into a table and you cannot presume to know the internal order in a relational database The original ordered listing above was obtained with a SQL statement that had an ORDER BY in it like this:... (x.test like ' %3' and x.tnum = 3) ) The above query would result in this display, indicating tests taken out of order by Jake: NAME -Jake Jake 100 TEST SCORE DT TNUM - -Test2 555 22-Dec-2006 12:15 1 Test1 735 22-Dec-2006 14 :33 2 Chapter | 3 NTILE An analytical function closely related to the ranking and row-counting functions is NTILE NTILE groups data by sort order into a variable... this data in it: SELECT * FROM empwnulls 86 Chapter | 3 Giving: EMPNO 101 102 104 108 111 106 122 ENAME -John Stephanie Christina David Katie Chloe Lindsey HIREDATE ORIG_SALARY CURR_SALARY - - 02-DEC-97 35 000 22-SEP-98 35 000 44000 08-MAR-98 430 00 55000 08-JUL-01 13- APR-00 45000 49000 19-JAN-96 33 000 44000 22-MAY-97 40000 52000 What effect will we see with the analytical functions. .. returned, and hence ORDER BY requires a sort To examine the procedure by which Oracle processes queries, we can look at the EXPLAIN PLAN output (see the EXPLAIN PLAN sidebar) 80 Chapter | 3 The EXPLAIN PLAN Output The EXPLAIN PLAN command may be used to find out how the Oracle Optimizer processes a statement The Optimizer is a program that examines the SQL statement as presented by a user and then devises . Programmer 1 33 000 33 000 1 Chemist 1 35 000 35 000 2 Accountant 1 35 000 35 000 2 75 Chapter | 3 Using HAVING with anUsing HAVING with an Analytical FunctionAnalytical Function Finally, if a HAVING clause. 101 John 35 000212 106 Chloe 33 000 131 104 Christina 430 00675 108 David 37 0004 13 111 Katie 45000756 122 Lindsey 40000564 102 Stephanie 35 00 033 2 79 Chapter | 3 RNUM in this case is the ordering of. Computer Programmer 1 33 000 33 000 Accountant 1 35 000 35 000 Chemist 1 35 000 35 000 Director Personnel 1 40000 40000 Mediator 3 40000 35 000 The ORDER BY is applied last. 73 Chapter | 3 Adding an Analytical

Ngày đăng: 08/08/2014, 18:21

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