with 语句

with 语句优点:一次定义多次使用,不用重复解析,结果存放在临时表中.

 

The WITH query_name clause lets you assign a name to a subquery       block. You can then reference the subquery block multiple places in the query by specifying the query name. Oracle      optimizes the query by treating the query name as either an inline view or as a temporary table.
with 语句可以让我们给子查询语句块定义一个名字,然后可以使用这个名字代替该语句块在多处使用,oracle将with语句的结果当着内部视图或临时表

You can specify this clause in any top-level SELECT statement and in most types of subqueries. The query name is       visible to the main query and to all subsequent subqueries except the subquery that  defines the query name itself.
我们可以在最顶层的select声明中或子查询中定义with语句,并且其作用域是全局的(with内部不能用他自身)

A WITH clause is really best used when the result of the WITH query is required more than one time in the body of the       query such as where one averaged value needs to be compared against two or three times.
当一个子查询在多处被使用时最好使用with语句代替,例如:在多个地方要求平均数用于比较时。

 

WITH <alias_one> AS
   (subquery_sql_statement),
      <alias_two> AS
   (sql_statement_from_alias_one)
SELECT <column_name_list>
FROM <alias_one>, <alias_two>
WHERE <join_condition>;

WITH qb1 AS
   (SELECT inst_id FROM gv$session),
       qb2 AS
   (SELECT unique inst_id FROM qb1
    UNION ALL
    SELECT unique inst_id FROM qb1)
SELECT /*+ MATERIALIZE */ *
FROM qb1, qb2
WHERE qb1.inst_id = qb2.inst_id;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值