oracle with 查询,oracle with 子查询用法

subquery_factoring_clause

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 Database optimizes the query by treating the query name as either an inline view or as a temporary table.

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.

Restrictions on Subquery FactoringThis clause is subject to the following restrictions:You can specify only one subquery_factoring_clause in a single SQL statement. You cannot specify a query_name in its own subquery. However, any query_name defined in the subquery_factoring_clause can be used in any subsequent named query block in the subquery_factoring_clause.

In a compound query with set operators, you cannot use the query_name for any of the component queries, but you can use the query_name in the FROM clause of any of the component queries.[@more@]

oracle 给出的一个示例:

Subquery Factoring: ExampleThe following statement creates the query names dept_costs and avg_cost for the initial query block containing a join, and then uses the query names in the body of the main query.WITH

dept_costs AS (

SELECT department_name, SUM(salary) dept_total

FROM employees e, departments d

WHERE e.department_id = d.department_id

GROUP BY department_name),

avg_cost AS (

SELECT SUM(dept_total)/COUNT(*) avg

FROM dept_costs)

SELECT * FROM dept_costs

WHERE dept_total >

(SELECT avg FROM avg_cost)

ORDER BY department_name;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值