`
z_kanhai
  • 浏览: 49359 次
  • 性别: 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用法介绍

    主要介绍了Python中的with...as用法介绍,本文直接给出用法实例,需要的朋友可以参考下

    oracle数据库with_as用法

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

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

    本篇文章主要介绍了python语言中with as的用法使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    python with (as)语句实例详解

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

    Python中的with…as用法介绍

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

    python-Keywords关键字说明(作业)

    and:逻辑‘与’ or:逻辑‘或’ not:逻辑‘非’ del:操作列表,用于删除列表一个...2. 与with组合,即with as用法,with后紧跟的对象,会调用它的__enter__方法,返回对象会赋值给temp: with objject as temp: pass

    详解Python with/as使用说明

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

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

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

    Python with的用法

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

    Python with用法实例

    主要介绍了Python with用法实例,本文讲解了with语句的几种使用方法和使用场景,需要的朋友可以参考下

    javascript中eval和with用法实例总结

    本文实例讲述了javascript中eval和with用法。分享给大家供大家参考,具体如下: 我们都知道javascript的作用域机制,但是with和eval有时会“破坏”我们对于作用域的常规理解。下面参考网上资源和自己理解总结一下...

    Javascript中With语句用法实例

    本文实例讲述了Javascript中With语句用法。分享给大家供大家参考。具体如下: <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8...

    python中with用法讲解

    with open() as f: 这两种方法的区别就是第一种方法需要我们自己关闭文件;f.close(),而第二种方法不需要我们自己关闭文件,无论是否出现异常,with都会自动帮助我们关闭文件,这是为什么呢? 我们先自定义一个类...

    Python中with及contextlib的用法详解

    本文实例讲述了Python中with及contextlib的用法。分享给大家供大家参考,具体如下: 平常Coding过程中,经常使用到的with场景是(打开文件进行文件处理,然后隐式地执行了文件句柄的关闭,同样适合socket之类的,...

    javascript中的遍历for in 以及with的用法

    with语句: (对象操作语句)  功能:为一段程序建立默认对象  格式: 代码如下:  with(<对象>){  <语句组>  } 具体示例: 代码如下: [removed] function member(name,gender){  this.name=name;  ...

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

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

Global site tag (gtag.js) - Google Analytics