当前位置: 首页 > news >正文

oracle with

oracle with

 

1)创建测试环境:

 

   DROP TABLE students;
   CREATE TABLE students(ID NUMBER, NAME VARCHAR2(20));
  
   INSERT INTO students SELECT ROWNUM +9064,'student_name',ROWNUM+20 FROM dual CONNECT BY        ROWNUM<50;
   COMMIT;
  
  
   DROP TABLE stu_score;
   CREATE TABLE stu_score(ID number, stu_id NUMBER, subject VARCHAR2(20), score NUMBER);
  
   INSERT INTO stu_score SELECT ROWNUM+10150,ROWNUM+9100,'数学',90 FROM dual CONNECT BY ROWNUM<50
   COMMIT;

 

2)使用with ... as

 

   WITH stu_age AS (SELECT * FROM students WHERE age > 20),
        score_sub AS (SELECT * FROM stu_score WHERE subject = '数学')
   SELECT a.id, a.name, b.subject, b.score
     FROM stu_age a, score_sub b
    WHERE a.id = b.stu_id;

 

Oracle WITH clause

Oracle Tips by Burleson Consulting

 

 

About Oracle WITH clause
Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.

The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:

   • The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
   • Formally, the “WITH clause” is called subquery factoring
   • The SQL “WITH clause” is used when a subquery is executed multiple times
   • Also useful for recursive queries (SQL-99, but not Oracle SQL)

To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);


Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH  clause”:

WITH
sum_sales AS
  select /*+ materialize */
    sum(quantity) all_sales from stores
number_stores AS
  select /*+ materialize */
    count(*) nbr_stores from stores
sales_by_store AS
  select /*+ materialize */
  store_name, sum(quantity) store_sales from
  store natural join sales
SELECT
   store_name
FROM
   store,
   sum_sales,
   number_stores,
   sales_by_store
where
   store_sales > (all_sales / nbr_stores)
;


Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.

It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.

To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);


Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:
 
 

 

 

相关文章:

  • 图片选择器ImageEditContainer
  • ETL基本概念
  • python学习笔记5-python的函数
  • oracle物化视图(materialized view)
  • 绝世好题bzoj4300
  • 在存储过程中拥有role的权限(Authid Current_User)
  • 渐变维度(Slowly Changing Dimension)及其处理方法
  • 获取url值
  • 1.Angular框架-angular介绍与基本使用,MVC模式介绍
  • Oracle性能调优-优化排序操作
  • 鲜为人知的软件项目管理原则
  • 业界经典语录
  • 数据仓库设计的思考
  • ubuntu下安装Java并设置环境变量
  • arguments、this指针、call和apply、bind语法
  • Google 是如何开发 Web 框架的
  • 【402天】跃迁之路——程序员高效学习方法论探索系列(实验阶段159-2018.03.14)...
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • 【翻译】babel对TC39装饰器草案的实现
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • ES6 ...操作符
  • MySQL几个简单SQL的优化
  • SpringBoot 实战 (三) | 配置文件详解
  • Terraform入门 - 3. 变更基础设施
  • 程序员最讨厌的9句话,你可有补充?
  • 将 Measurements 和 Units 应用到物理学
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 前嗅ForeSpider教程:创建模板
  • 入门到放弃node系列之Hello Word篇
  • 时间复杂度与空间复杂度分析
  • 源码安装memcached和php memcache扩展
  • 你对linux中grep命令知道多少?
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • (70min)字节暑假实习二面(已挂)
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (翻译)terry crowley: 写给程序员
  • (附源码)spring boot校园健康监测管理系统 毕业设计 151047
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (算法)Game
  • (转)Android学习系列(31)--App自动化之使用Ant编译项目多渠道打包
  • (转)jQuery 基础
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (转载)跟我一起学习VIM - The Life Changing Editor
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .NET BackgroundWorker
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .Net MVC4 上传大文件,并保存表单
  • .NET 常见的偏门问题
  • .NET/C# 避免调试器不小心提前计算本应延迟计算的值
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • @ 代码随想录算法训练营第8周(C语言)|Day53(动态规划)
  • @Bean有哪些属性