`
z_kanhai
  • 浏览: 51559 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

with as 用法

 
阅读更多
查看( 15 ) / 评论( 0 ) / 评分( 0 / 0 )
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语句起到一个优化的作用,而且清楚明了




这是搜索到的英文文档资料(说得比较全,但是本人英文特菜,还没具体了解到,希望各高手具体谈谈这个with
as 的好处)

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 /*+ materialize */
    sum(quantity) all_sales from stores
number_stores AS
  select /*+ materialize */
    count(*) nbr_stores from stores
sales_by_store AS
  select /*+ materialize */
  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 with as用法详解

    一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ...

    Python中的with...as用法介绍

    下面是一个简单的文件操作示例,展示了`with...as`的用法: ```python with open("/tmp/foo.txt") as file: data = file.read() ``` 在这个例子中: 1. `open("/tmp/foo.txt")`表达式被求值,返回一个文件对象。 ...

    oracle数据库with_as用法

    详细介绍oracle数据库中新出的with_as语法以及相关使用

    python语言中with as的用法使用详解

    with expression as variable: block ``` 这里的 `expression` 是一个可调用对象,通常是一个打开文件、数据库连接或其他需要初始化的资源。`as variable` 是可选的,用于在 `with` 代码块内部引用该资源。 在 `...

    oracle数据库startwith用法

    ### Oracle数据库中的START WITH 和 CONNECT BY 用法详解 在Oracle数据库中,处理层次结构数据时,`START WITH` 和 `CONNECT BY` 是非常有用的两个关键字。这些关键字可以帮助我们在查询时构建出树形或者层级结构的...

    python with (as)语句实例详解

    这篇文章主要介绍了python with (as)语句实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 with语句适用于对资源进行访问的场合,确保不管使用过程中...

    Python中的with…as用法介绍

    with EXPRESSION [ as VARIABLE] WITH-BLOCK 基本思想是with所求值的对象必须有一个__enter__()方法,一个__exit__()方法。 紧跟with后面的语句被求值后,返回对象的__enter__()方法被调用,这个方法的返回值将被...

    WITH CHECK OPTION的用法

    本文将深入探讨WITH CHECK OPTION的用法及其背后的原理,帮助读者更好地理解并掌握这一特性。 ### 什么是WITH CHECK OPTION? 在创建视图时,可以通过添加WITH CHECK OPTION子句来指定视图的某些约束条件。这个...

    Python with用法:自动关闭文件进程

    with context expression [as target(s)]: with 代码块 在上面的语法格式中,context_expression 用于创建可自动关闭的资源。 例如,程序使用 with 语句来读取文件: import codecs # 使用with语句打开文件,该...

    详解Python with/as使用说明

    with/as 使用open打开过文件的对with/as都已经非常熟悉,其实with/as是对try/finally的一种替代方案。 当某个对象支持一种称为”环境管理协议”的协议时,就会通过环境管理器来自动执行某些善后清理工作,就像...

    as-to和as-for的用法.ppt

    在英语中,"as-to"和"as-for"是两个常用的介词短语,它们在句子中的用法和含义略有不同。以下是对这两个短语的详细解析: 首先,"as for"主要用于转换话题或者强调某个特定的观点。它可以出现在句首,以引入与前文...

    Mssql 三种临时表的使用场景 insert into 与 select into与With as.docx

    临时表分为三种类型:本地临时表(以#开头),全局临时表(以##开头)以及行集函数中的表变量(如WITH AS)。在本文中,我们将探讨这三种临时表的使用场景,以及`INSERT INTO`、`SELECT INTO`和`WITH AS`语句的应用...

    Python with的用法

    with open('cardlog.txt','r') as item : for line in item : print line;    在file的结束,会自动关闭该文件句柄。   在python2.6中,with正式成为了关键字 所以在python2.5以前,要利用with的话,需要使用...

    python with语句的原理与用法详解

    with expression [as target]: suite ``` 这里的 `expression` 是一个产生上下文管理器的对象,它可以是一个函数调用或者类的实例。`as target` 部分是可选的,如果存在,`__enter__()` 方法返回的结果将会被赋值给...

    oracle 下WITH CHECK OPTION用法

    Oracle 下 WITH CHECK OPTION 用法 WITH CHECK OPTION 是 Oracle 中的一种视图定义选项,它可以确保数据库中正在修改的数据的完整性。该选项通常用在视图定义中,以确保任何引用该视图的 INSERT 或 UPDATE 语句都...

    Python with用法实例

    当使用`with A() as a:`时,`__enter__()`会在进入`with`块之前被调用,`__exit__()`则在退出`with`块时调用,参数`e_type`, `e_value`, `traceback`用于处理异常情况。 Python的`contextlib`模块提供了更高级的上...

    python中as用法实例分析

    在这个例子中,`open`函数返回了一个文件对象,我们通过`as`关键字将其绑定到变量`file`上,然后在`with`语句块内使用`file`来读取文件内容。 #### 四、总结 通过上述介绍可以看出,`as`关键字在Python编程中扮演...

    Oracle分组函数之ROLLUP的基本用法

    本博客简单介绍一下oracle分组函数之rollup的用法,rollup函数常用于分组统计,也是属于oracle分析函数的一种 环境准备 create table dept as select * from scott.dept; create table emp as select * from ...

    Android中Glide获取图片Path、Bitmap用法详解

    软件开发网在此之前给大家介绍过图片加载...Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget() { @Override public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> gl

Global site tag (gtag.js) - Google Analytics