1. 程式人生 > >oracle功能強大的with子句

oracle功能強大的with子句

statements

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
87788SCOTT
ANALYST756619-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子句