先举个例子吧:
有两张表,分别为A、B,求得一个字段的值先在表A中寻找,如果A表中存在数据,则输出A表的值;如果A表中不存在,则在B表中寻找,若B表中有相应记录,则输出B表的值;如果B表中也不存在,则输出"no records”字符串。
- with
- sql1 as (select to_char(a) s_name from test_tempa),
- sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1))
- select * from sql1
- union all
- select * from sql2
- union all
- select 'no records' from dual
- where not exists (select s_name from sql1 where rownum=1)
- and not exists (select s_name from sql2 where rownum=1);
再举个简单的例子
with a as (select * from test)
select * from a;
其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它
这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了
下面是搜索到的英文文档资料
About Oracle WITH clause
Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.
The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:
• The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
• Formally, the “WITH clause” is called subquery factoring
• The SQL “WITH clause” is used when a subquery is executed multiple times
• Also useful for recursive queries (SQL-99, but not Oracle SQL)
To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.
The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH clause”:
WITH
sum_sales AS
select
sum(quantity) all_sales from stores
number_stores AS
select
count(*) nbr_stores from stores
sales_by_store AS
select
store_name, sum(quantity) store_sales from
store natural join sales
SELECT
store_name
FROM
store,
sum_sales,
number_stores,
sales_by_store
where
store_sales > (all_sales / nbr_stores)
;
Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.
It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.
To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy
The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);
Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:
=================================================================================
下面自己小试一把,当然,一点都不复杂,很简单很简单的例子,呵呵。
- SQL> create table t2(id int);
-
- Table created.
-
- SQL> create table t3(id int);
-
- Table created.
-
- SQL> insert into t2 values(1);
-
- 1 row created.
-
- SQL> insert into t2 values(2);
-
- 1 row created.
-
- SQL> insert into t3 values(3);
-
- 1 row created.
-
- SQL> commit;
-
- Commit complete.
-
- SQL> select * from t2;
-
- ID
- ----------
- 1
- 2
-
- SQL> select * from t3;
-
- ID
- ----------
- 3
- SQL> with
- 2 sql1 as (select * from t2),
- 3 sql2 as (select * from t3)
- 4 select * from t2
- 5 union
- 6 select * from t3;
- sql2 as (select * from t3)
- *
- ERROR at line 3:
- ORA-32035: unreferenced query name defined in WITH clause
-
- --从这里可以看到,你定义了sql1和sql2,就得用它们哦,不然会报错的。
-
- SQL> with
- 2 sql1 as (select * from t2),
- 3 sql2 as (select * from t3)
- 4 select * from sql1
- 5 union
- 6 select * from sql2;
-
- ID
- ----------
- 1
- 2
- 3
-
- --下面加个WHERE条件试试
-
- SQL> with
- 2 sql1 as (select * from t2),
- 3 sql2 as (select * from t3)
- 4 select * from sql1
- 5 union
- 6 select * from sql2
- 7 where id in(2,3);
-
- ID
- ----------
- 1
- 2
- 3
-
- --奇怪?为什么加了WHERE条件还是输出ID=1的记录了,继续往下看:
-
- SQL> with
- 2 sql1 as (select * from t2),
- 3 sql2 as (select * from t3)
- 4 select * from sql1
- 5 where id=3
- 6 union
- 7 select * from sql2
- 8 where id=3;
-
- ID
- ----------
- 3
-
- --可以看到,每个条件是要针对每个SELECT语句的。
好了就先记这些吧,以后看到了新的用法再补充。
分享到:
相关推荐
详细介绍oracle数据库中新出的with_as语法以及相关使用
通过本文,我们详细介绍了Oracle数据库中 `START WITH` 和 `CONNECT BY` 的使用方法以及应用场景。这两个关键字对于处理具有层级结构的数据非常有用。此外,我们还讨论了如何利用 `LEVEL` 关键字来显示节点所在的...
Oracle 下 WITH CHECK OPTION 用法 WITH CHECK OPTION 是 Oracle 中的一种视图定义选项,它可以确保数据库中正在修改的数据的完整性。该选项通常用在视图定义中,以确保任何引用该视图的 INSERT 或 UPDATE 语句都...
本博客简单介绍一下oracle分组函数之rollup的用法,rollup函数常用于分组统计,也是属于oracle分析函数的一种 环境准备 create table dept as select * from scott.dept; create table emp as select * from ...
`START WITH...CONNECT BY PRIOR`是Oracle SQL中的一个特性,用于处理具有层级关系的数据。这个子句允许我们遍历和查询具有父子关系的数据,例如部门和其下属子部门,或者员工和他们的上级经理。 1. **START WITH...
代码如下:with alias_name1 as (subquery1), alias_name2 as (subQuery2), …… alias_nameN as (subQueryN) select col1,col2…… col3from alias_name1,alias_name2……,alias_nameN Oracle with语句的...
下面我们将详细讲解DECODE函数的使用方法以及如何在列转行操作中应用它。 DECODE函数的基本语法如下: ```sql DECODE(column, value1, result1, value2, result2, ..., default_result) ``` 这里`column`是要检查的...
Oracle 中抽取随机数的多种方法 在 Oracle 中抽取随机数是许多应用场景中常见的问题,例如在某个活动中需要随机取出一些符合条件的用户,以颁发获奖通知或其它消息。本文将通过实例讲解如何抽取随机数的多种方法。 ...
Oracle 11GR2的递归WITH子查询是一种强大的功能,它允许你在SQL查询中创建递归关系,处理层级数据,比如组织结构、家族树或依赖关系等。WITH子查询,也称为公共表表达式(CTE),是SQL标准的一部分,自9i版本开始被...
- 使用`ALTER TABLE TableName ACTIVE NOT LOGGED INITIALLY WITH EMPTY TABLE;` - 这个操作在DB2中也能高效地清空表。 #### 6. 关于ROWID - **Oracle**: - ROWID是Oracle数据库提供的一个特殊功能,用于唯一...
除了使用 DBMS_RANDOM 包外,还有其他方法可以生成随机数,例如使用 Oracle 的内置函数、使用 PL/SQL 语言等。这些方法可以根据实际情况选择。 Oracle 提供了多种方法来生成随机数,每种方法都有其优缺点,选择合适...
搜索"Oracle.ManagedDataAccess",然后安装,这样你就可以在代码中使用Oracle相关的类和方法了。 接下来,我们来看看如何建立一个基本的数据库连接。在VB.NET中,这通常通过创建`OracleConnection`对象实现。以下是...
- Oracle 的 DECODE 方法提供了一种根据条件返回不同值的方法。 - DB2 中没有内置的 DECODE 函数,但可以使用 CASE 语句实现类似功能。 10. 其他差异: - 存储过程和函数的编写语法有所不同。 - 视图、索引、...
### ORACLE去除重复数据方法 在数据库管理与维护过程中,数据重复问题是非常常见的现象,尤其在大型企业级应用中更是如此。重复数据不仅占用存储空间,还可能导致数据分析结果出现偏差,影响业务决策的准确性。因此...
SELECT ename, REGEXP_LIKE(ename, '^[0-9]') AS starts_with_number FROM emp; ``` 这个查询将返回一个标志,表明员工名字是否以数字开始。 了解并熟练掌握Oracle分析函数和正则表达式函数,能显著提高在数据库...
接着,可以创建一个快照,如`CREATE SNAPSHOT table3beijing REFRESH FORCE START WITH SYSDATE NEXT SYSDATE+1/24 WITH PRIMARY KEY AS SELECT * FROM table3@beijing`,这会定时强制刷新快照,确保数据的一致性。...
WITH consecutive_dates AS ( SELECT employee_id, attendance_date, LAG(attendance_date, 1) OVER (PARTITION BY employee_id ORDER BY attendance_date) AS prev_date, LEAD(attendance_date, 1) OVER ...
使用JDBC时,可以结合LIMIT和OFFSET子句(Oracle不支持LIMIT,但可以通过ROWNUM模拟)来实现分页,而在Hibernate中,可以使用Criteria API或HQL的`setFirstResult`和`setMaxResults`方法来实现相同的功能。...
此外,还可以利用`with`语句来自动管理连接和游标,确保资源在使用完毕后会被正确关闭。 通过这个`OracleHelper`类,我们可以方便地进行Python对Oracle数据库的各种操作,实现数据库操作的简化和模块化。在开发过程...
### Oracle高级SQL培训与讲解——WITH子句详解 #### 学习目标 - 掌握WITH子句的基本用法及特点。 - 了解WITH子句如何优化查询性能。...熟练掌握WITH子句的使用方法,对于提高数据库查询技能至关重要。