Oracle uses with as to create temporary tables

1. Business needs

        In the development process of the oracle project, after using sql to write the corresponding analysis report content, some lines involved in the sql analysis report are lost, resulting in the dislocation of the statistical line data that is fixedly marked with eye-catching colors when the report is classified and counted; Therefore, it is necessary to repair the complete line classification of the analysis report.

2. Business analysis

        After writing the corresponding analysis report content in sql, due to the missing data of some lines, the report shows that the lines with missing data are not displayed; now it is necessary to display the lines with missing data. The easiest way is to right-join the existing sql statement to a fixed-line temporary table (but use the with as in oracle to create a fixed-line temporary table).

3. Implementation method

3.1, With as usage

Selecting from the DUAL Table (oracle.com)icon-default.png?t=N3I4https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Selecting-from-the-DUAL-Table.html

13.2.20 WITH (Common Table Expressions) (oracle.com)icon-default.png?t=N3I4https://docs.oracle.com/cd/E17952_01/mysql-8.0-en/with.html#common-table-expressions

SQL | Join (Inner, Left, Right and Full Joins) - GeeksforGeeksicon-default.png?t=N3I4https://www.geeksforgeeks.org/sql-join-set-1-inner-left-right-and-full-joins/ SQL JOIN 连接_w3cschoolicon-default.png?t=N3I4https://www.w3cschool.cn/sql/2nmo1oyt.html

3.2, with as to create a temporary table

① Create a temporary table syntax

with 临时表名称 as(

SELECT 值1 列名1,值2 列名2 from dual union all
SELECT 值1 列名1,值2 列名2 from dual union all
SELECT 值1 列名1,值2 列名2 from dual 
)
SELECT * from 临时表名称

② Create a temporary table example

with TEMPTABLE as(

SELECT 1 LINE, '一线' NOTES from dual union all
SELECT 2 LINE, '二线' NOTES from dual union all
SELECT 3 LINE, '三线' NOTES from dual 
)
SELECT * from TEMPTABLE

Example execution results are as follows:

 

Guess you like

Origin blog.csdn.net/xiaochenXIHUA/article/details/130662855