1、with table as 相当于建个临时表(用于一个语句中某些中间结果放在临时表空间的sql语句),oracle 9i 新增with语法,可以将查询中的子查询命名,放到select语句的最前面。
语法就是
with tempname as (select ....)
select ...
例子:
with t as (select * from emp where depno=10)
select * from t where empno=xxx
with
wd as (select did,arg(salary) 平均工资 from work group by did),
em as (select emp.*,w.salary from emp left join work w on emp.eid = w.eid)
select * from wd,em where wd.did =em.did and wd.平均工资>em.salary;
2、何时被清除
临时表不都是会话结束就自动被pga清除嘛! 但with as临时表是查询完成后就被清除了!
23:48:58 scott@orcl> with aa as(select * from dept)
23:57:58 2 select * from aa;
deptno dname loc
---------- -------------- -------------
10 accounting new york
20 research dallas
30 sales chicago
40 operations boston
已用时间: 00: 00: 00.12
23:58:06 scott@orcl> select * from aa;
select * from aa
*
第 1 行出现错误:
ora-00942: 表或视图不存在
已用时间: 00: 00: 00.02
23:58:14 scott@orcl>
3、就这一功能来说,子查询就可以达到啊,为什么要用with呢? 用with有什么好处?
都能写,但执行计划不同的。当有多个相似子查询的时候,用with写公共部分,因为子查询结果在内存临时表中,执行效率当然就高啦~
4、问题:
有张表数据如下:
aaa 高
bbb 低
aaa 低
aaa 高
bbb 低
bbb 高
需要得到下列结果,
高 低
aaa 2 1
bbb 1 2
问 sql 语句怎么写??
答案:
with tt as (
select 'aaa' id, '高' value from dual union all
select 'bbb' id, '低' value from dual union all
select 'aaa' id, '低' value from dual union all
select 'aaa' id, '高' value from dual union all
select 'bbb' id, '低' value from dual union all
select 'bbb' id, '高' value from dual)
select id,
count(decode(value, '高', 1)) 高,
count(decode(value, '低', 1)) 低
from tt
group by id;
===================================================================
扩展:
oracle9i新增with语法,可以将查询中的子查询命名,放到select语句的最前面。
一个简单的例子:
sql> with
2 seg as (select segment_name, sum(bytes)/1024 k from user_segments group by segment_name),
3 obj as (select object_name, object_type from user_objects)
4 select o.object_name, object_type, nvl(s.k, 0) size_k
5 from obj o, seg s
6 where o.object_name = s.segment_name ( )
7 ;
object_name object_type size_k
------------------------------ ------------------- ----------
daijc_test table 128
p_test procedure 0
ind_daijc_test_c1 index 128
通过with语句定义了两个子查询seg和obj,在随后的select语句中可以直接对预定义的子查询进行查询。从上面的例子也可以看出,使用with语句,将一个包含聚集、外连接等操作sql清晰的展现出来。
with定义的子查询不仅可以使查询语句更加简单、清晰,而且with定义的子查询还具有在select语句的任意层均可见的特点。
即使是在with的定义层中,后定义的子查询都可以使用前面已经定义好的子查询:
sql> with
2 q1 as (select 3 5 s from dual),
3 q2 as (select 3 * 5 m from dual),
4 q3 as (select s, m, s m, s * m from q1, q2)
5 select * from q3;
s m s m s*m
---------- ---------- ---------- ----------
8 15 23 120
利用with定义查询中出现多次的子查询还能带来性能提示。oracle会对with进行性能优化,当需要多次访问with定义的子查询时,oracle会将子查询的结果放到一个临时表中,避免同样的子查询多次执行,从而有效的减少了查询的io数量。
with能用在select语句中,update和delete语句也是支持with语法的,只是需要版本支持:
http://www.oracle.com.cn/viewthread.php?tid=83530
=============================================================================
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”:
posted on 2011-12-06 16:47
紫蝶∏飛揚↗ 阅读(22838)
评论(0) 编辑 收藏 所属分类:
数据库