oracle功能强大的with子句

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle功能强大的with子句相关的知识,希望对你有一定的参考价值。

select * from emp ;



EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
17369SMITHCLERK790217-12月-80800
20
27499ALLENSALESMAN769820-2月 -81160030030
37521WARDSALESMAN769822-2月 -81125050030
47566JONESMANAGER783902-4月 -812975
20
57654MARTINSALESMAN769828-9月 -811250140030
67698BLAKEMANAGER783901-5月 -812850
30
77782CLARKMANAGER783909-6月 -812450
10
87788SCOTTANALYST756619-4月 -873000
20
97839KINGPRESIDENT
17-11月-815000
10


select * from dept ;



DEPTNODNAMELOC
110ACCOUNTINGNEW YORK
220RESEARCHDALLAS
330SALESCHICAGO
440OPERATIONSBOSTON


功能强大的WITH子句的用法

WITH 
dept_costs AS (
SELECT d.dname, SUM(e.sal) AS dept_total 
FROM emp e, dept d 
WHERE e.deptno = d.deptno 
GROUP BY d.dname ),
avg_cost AS (
SELECT SUM(dept_total)/COUNT(*) AS dept_avg FROM dept_costs ) 
SELECT * FROM dept_costs 
WHERE dept_total < 
(SELECT dept_avg FROM avg_cost) 
ORDER BY dname ;

官方解释:

The WITH Clause Usage Notes
It is used only with SELECT statements.
A query name is visible to all WITH element query blocks (including their subquery blocks)
defined after it and the main query block itself (including its subquery blocks).
When the query name is the same as an existing table name, the parser searches from the inside
out, the query block name takes precedence over the table name.
The WITH clause can hold more than one query. Each query is then separated by a comma.

 


以上是关于oracle功能强大的with子句的主要内容,如果未能解决你的问题,请参考以下文章

Oracle WITH 子句不返回数据

Oracle 使用 with 子句创建表

Oracle 12c 子查询的 WITH 子句中的函数

oracle查询中的“WITH”子句优化

Oracle Reports 中的从属 WITH 子句

ORACLE 11G 中的 WITH 子句使用 UNION ALL