-----****************************with..as oracle usage --grammar: with tempName as (select ....) select ... --Example: Now we want to get 11-14 from 1-19. The general sql is as follows: select * from ( -- Simulate a 20-line data SELECT LEVEL AS lv FROM DUAL CONNECT BY LEVEL < 20 ) tt WHERE tt.lv > 10 AND tt.lv < 15 -- The SQL using With as is: with TT as( -- Simulate a 20-line data SELECT LEVEL AS lv FROM DUAL CONNECT BY LEVEL < 20 ) select lv from TT WHERE lv > 10 AND lv < 15 /*With query statement does not start with select, but with the keyword "WITH" It can be considered that a temporary table TT is pre-constructed before the actual query, and then it can be used many times for further analysis and processing Advantages of WITH Clause Method Increases the readability of SQL. If multiple subqueries are constructed, the structure will be clearer; more importantly: "Analyze once, use many times", This is also where performance is provided, achieving the goal of "less reads". The first method using a subquery table is scanned twice, while with the WITH Clause method the table is scanned only once. This can greatly improve the efficiency of data analysis and query. In addition, observe the execution plan of the WITH Clause method, in which "SYS_TEMP_XXXX" is the temporary table of intermediate statistical results constructed during the operation. */ -----**************************** Use WITH statement in the view to connect CREATE OR REPLACE VIEW WITH_V AS WITH DEPT_V AS (SELECT * FROM DEPT), EMP_V AS (SELECT * FROM EMP) SELECT D.DNAME,D.LOC,E.* FROM EMP_V E LEFT JOIN DEPT_V D ON D.DEPTNO = E.DEPTNO -----**************************** Example of using the WITH statement: /* Query the departments whose 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, you must first calculate the total salary of all departments, and then calculate the average salary of the total salary, Then filter out the departments whose total salary is greater than the average salary of all departments. Then step 1 with query finds out the total salary of all departments, The second step is to use with to query the average salary from the result table obtained in the first step, and finally use the two with queries to compare the results that the total salary is greater than the average salary, as follows: */ WITH DEPT_COSTS AS -- Query the total salary of the department (SELECT D.DNAME, SUM(E.SAL) DEPT_TOTAL FROM DEPT D, EMP E WHERE E.DEPTNO = D.DEPTNO GROUP BY D.DNAME), AVE_COST AS -- Query the average salary of the department, you can refer to the previously defined WITH statement in the latter WITH statement (SELECT SUM(DEPT_TOTAL) / COUNT(*) AVG_SUM FROM DEPT_COSTS) SELECT -----************************* /* First, the meaning of WITH AS The WITH AS phrase, also known as subquery factoring, defines an SQL fragment that will be used by the entire SQL statement. Especially useful for UNION ALL. Because each part of UNION ALL may be the same, but if each part is executed once, the cost is too high, So you can use the WITH AS phrase, you only need to execute it once. If the table name defined by the WITH AS phrase is called more than twice, the optimizer automatically assigns WITH AS The data obtained by the phrase is put into a TEMP table, if it is only called once, it will not. The prompt materialize is to force the data in the WITH AS phrase into the in a global temporary table. Many queries can be speeded up this way. 2. Examples of the use of WITH AS Query a field from the A table, if it is not queried from the B table, if there is neither in the A nor the B table, it will output ERROR */ with sql1 as (select to_char(id) myid from a), sql2 as (select to_char(id) myid from b where not exists (select * from sql1 where rownum > 5)) select * from sql1 union all select * from sql2 union all select 'error' from dual where not exists (select * from sql1 where rownum > 5) and not exists (select * from sql2 where rownum > 2);