首页 > 技术文章 > Oracle with重用子查询

john2017 2017-02-06 19:50 原文

--with 重用子查询
对于多次使用相同子查询的复杂查询语句来说,用户可能会将查询语句分成两条语句执行。第一条语句将子查询结果存放到临时表,第二条查询语句使用临时表处理数据。从 Oracle 9i 开始,通过 with 子句可以给予子查询指定一个名称,并且使得在一条语句中可以完成所有任务,从而避免了使用临时表。


SCOTT@ test10g> with summary as (
  2  select dname, sum(sal) dept_total from emp, dept
  3  where emp.deptno=dept.deptno group by dname
  4  )
  5  select dname,dept_total from summary
  6  where dept_total>
  7  (select sum(dept_total)*1/3 from summary);

DNAME          DEPT_TOTAL
-------------- ----------
RESEARCH            10875

推荐阅读