Article Source: http://blog.163.com/[email protected]/blog/static/49479943201171710305298/thank the author for sharing
3.1 With Basics
Use the WITH AS statement to define a name for a subquery block, which can be referenced in many places in a query statement using the subquery name. The Oracle database treats the referenced subquery name as if it were an inline view or a temporary table, thereby optimizing the function. The WITH clause is the new syntax for 9i.
You can use a subquery to define clauses before any of the top-level SELECT statements and almost all types of subquery statements. A defined subquery name can be referenced in the main query statement and in all subquery statements, but not before it is defined.
Cannot nest definition in a with clause < that is, the WITH clause does not have a in clause, but a subquery definition statement that appears in a subquery can reference a defined subquery name. < can refer to the WITH clause already defined earlier >
Related summary with clause:
1. Using the WITH clause allows the subquery to reuse the same with query block, through a select call (with clause can only be referenced by the Select query block), typically used in multiple cases with a query. Defined before the referenced SELECT statement, the sibling can only define the WITH keyword to be used only once, and multiple separated by commas.
The return result of the 2.with clause is stored in the user's temporary table space, only one query, repeated use, improve efficiency.
3. When there are multiple query definitions before the peer Select, the 1th is used with, followed by without with, and separated by commas.
5. The last with clause cannot have a comma between the following query, only the right parenthesis, and the query with the clause must be enclosed in parentheses.
6. If the WITH clause is defined and is not used in the query, the ora-32035 error is reported: The query name defined in the WITH clause is not referenced. (The name of at least one with query is not referenced, the workaround is to remove the unreferenced with query), note: As long as there is a reference, it is not necessarily to be referenced in the main query, such as the following with the query is also quoted, it is possible.
7. The query defined by the preceding with clause can be used in a later with clause. However, a with clause cannot be nested inside a with clause.
8. When a query block name and a table name or other objects in the same time, the parser from the inside out, priority to use the subquery block name.
The results of a 9.with query are listed with an alias, and the reference must use an alias or *.
Advantages of the WITH clause:
1. SQL readability is enhanced. For example, a specific with subquery to take a meaningful name, and so on.
2. The WITH subquery executes only once, storing the results in the user temp table space, which can be referenced multiple times to enhance performance.
WITH clause syntax:
With Alias_name as (Select1), the parentheses in--as and select cannot be omitted
Alias_name2 as (select2),--without with, comma-separated, same primary query same level place, with subquery can only be defined once
...
Alias_namen as (select N) – no comma between the actual queries below
Select ....
With Use example:
1. General use mode
If you are checking the name of the sales department employee:
--with clause
With A As
(select ID from s_dept where name=sales the ORDER by ID)
Select Last_name,title
From S_emp where dept_id in (SELECT * from a);--Using Select to query aliases
Using the WITH clause, you can pre-define the good one result set in a complex query, and then use it repeatedly in queries, without using an error. And the WITH clause obtains a temporary table that, if used in a query, must use the select from with query name, such as
With CNT as (select COUNT (*) from table)
Select cnt+1 from dual;
Is wrong. Must be
With CNT as (select COUNT (*) Shumu from User_tables)
Select shumu+1 from CNT;
2. References in most subqueries, sibling visible
Then the following two statements mean equivalent:
With a as (select Trade_id,name from Product)--defined before use
Select name from a where a.trade_id in (
With B as (select ID from trademark where id=1)--defined before use
Select ID from b
);
Select name from the product where trade_id in (the Select ID from trademark where id=1);
3. The WITH subquery cannot be nested, but subsequent with definitions can refer to the preceding result set.
With Select_trade as (select trade_id from product where id=1),
--The following with subquery can refer to the previous result
Select_trademark as (select name from trademark where id= (select trade_id from Select_trade))
SELECT * from Select_trademark;
--this statement is wrong
With Select_trade as
Nested with in--with, not allowed
(with Temp as (select trade_id from product where id=1)
Select trade_id from Temp
),
Select_trademark as (select name from trademark where id= (select trade_id from Select_trade))
SELECT * from Select_trademark;
4. Sibling definition, only one with
With a as (select trade_id from product),
B as (select ID from trademark where rownum<3)--The 2nd definition does not have a with
Select trade_id from a
Minus
Select id from B;
5. Defined and used in most sub-queries, a complex example
SELECT A, b
From (
--1th definition of T_with
With
T_with as (SELECT 1 a from DUAL)
--sub-query using T_with
SELECT X.a, (
--The internal definition of a t_with_z, and the use of T_with
With T_with_z as (SELECT 1 a from T_with)
SELECT s_1.a from T_with_z s_1, T_with s_2
6. Using a with subquery in the collection
-This is wrong, the sibling can have only one with the definition, of course, two can be written together.
SELECT *
From (
With
T_with_1 as (SELECT 1 A, 1 b from DUAL)
--The entire statement thereafter is considered to be a main query statement
SELECT x.a, x.b from T_with_1 x
UNION ALL
With--Define again here
T_with_2 as (SELECT 1 A, 2 b from DUAL)
SELECT y.a, y.b from t_with_2 y
-Correct
SELECT *
From (
--1th A with
With
T_with_1 as (SELECT 1 A, 1 b from DUAL)
SELECT x.a, x.b, x C from t_with_1 x
UNION ALL
--cannot be defined here with a, only 1 with the sibling, can be written on the front
--Internal with
SELECT y.a, y.b, (with
T_with_x as (SELECT * from t_with_1)
SELECT A from t_with_x)
From (with
T_with_2 as (SELECT 1 A, 2 b from DUAL)
SELECT A, b from t_with_2
) y
WHERE y.a = (
With T_with_3 as (SELECT 1 A, 2 b from DUAL)
Select a from T_with_3
)
)
7. An example with a query:
Find out the department's total salary is greater than the average total salary of all departments. Department table s_dept, employee table s_emp. Analysis: To do this query, the first must calculate the total salary of all departments, and then calculate the average salary of the total salary, and then screen out the department's total salary is larger than the total salary of all departments of the Department of the average salary. So the 1th step with query to find out the total salary of all departments, 2nd step with the result table obtained from 1th step to find the average salary, and finally use these two times with the query to compare the total salary than the average salary results, as follows:
With
--STEP1: Find out the department name and department's total salary
Dept_costs as (
Select A.name,sum (b.salary) dept_total
From
S_dept A,s_emp b
where a.id=b.dept_id
GROUP BY A.name
),
--STEP2: Calculates the average total salary of the department with the results of the previous with query
Avg_costs as (
Select SUM (dept_total)/count (*) dept_avg
From Dept_costs
)
--STEP3: Compare and output query results from two with queries
Select Name,dept_total
From Dept_costs
where
Dept_total>
(
Select Dept_avg
From
Avg_costs
)
Order BY name;
As can be seen from the above query, the result of the previous with query can be reused by the following with query, and the result column of the with query supports the use of aliases, in the final query must refer to all with queries, otherwise error ora-32035 errors.
If there is a requirement: A query, if the result row of the query does not meet a multiple of 10, then fill the blank line until the number of rows queried is a multiple of 10. For example: SELECT * FROM trademark this query.
3.2 11G R2with new features
The new version of with allows recursive use, in a functionally more qualitative leap than the original, you can achieve many of the original connect by does not do.
3.3 With impact on the execution plan
The WITH clause may change the execution plan.
With temp as---The SQL statement usage goes