oracle功能強大的with子句

來源:互聯網
上載者:User

標籤:statements

select * from emp ;



EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 7369 SMITH CLERK 7902 17-12月-80 800
20
2 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
3 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
4 7566 JONES MANAGER 7839 02-4月 -81 2975
20
5 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
6 7698 BLAKE MANAGER 7839 01-5月 -81 2850
30
7 7782 CLARK MANAGER 7839 09-6月 -81 2450
10
8 7788 SCOTT ANALYST 7566 19-4月 -87 3000
20
9 7839 KING PRESIDENT
17-11月-81 5000
10


select * from dept ;



DEPTNO DNAME LOC
1 10 ACCOUNTING NEW YORK
2 20 RESEARCH DALLAS
3 30 SALES CHICAGO
4 40 OPERATIONS BOSTON


功能強大的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子句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.