`
kapok_fly
  • 浏览: 34554 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Oracle With 语法 示例

阅读更多

Oracle with语句示例


WITH a AS (SELECT * FROM bd_member WHERE ROWNUM<10),
b AS (SELECT * FROM tp_trade_card)
select A.MEMBER_NAME,
B.CARD_NO
from A,
B
where A.BD_MEMBER_ID = B.BD_MEMBER_ID

 

Google Search: Oracle +with 视图就可以得到结果...

 

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”:
 
分享到:
评论

相关推荐

    oracle while的用法示例分享

    当查询中多次用到某一部分时,可以用Oracle with语句创建一个公共临时表。因为子查询在内存临时表中,避免了重复解析,所以执行效率会提高不少。临时表在一次查询结束自动清除。 一般语法格式: 代码如下:with ...

    Oracle通过递归查询父子兄弟节点方法示例

    说到Oracle中的递归查询语法,我觉得有一些数据库基础的童鞋应该都知道,做项目的时候应该也会用到,下面本文就来介绍下关于Oracle通过递归查询父子兄弟节点的相关内容,分享出来供大家参考学习,下面话不多说了,来...

    OCPOCA认证考试指南全册:Oracle Database 11g(1Z0-051,1Z0-052,1Z0-053)--详细书签版(第2/2部分)

    原书名: OCA/OCP Oracle Database 11g All-in-One Exam Guide with CD-ROM: Exams 1Z0-051, 1Z0-052, 1Z0-053 原出版社: McGraw-Hill Osborne Media 作者: (美)John Watson Roopesh Ramklass Bob Bryla 译者: ...

    oracle学习经典教程

    1.3.2.1 插入: Insert with a subquery method ...........................46 1.3.2.1.1 Oracle 11g 的Interval................46 1.3.2.1.2 Oracle 10g 版本........................47 1.3.2.2 交换...

    Oracle9i的init.ora参数中文说明

    说明: 指定一对值 (UTC,TZD), 设置 TIME WITH TIME ZONE 数据类型的默认值, 该数据类型包含 HOUR, MINUTE, SECOND, TIMEZONE_HOUR 和 TIMEZONE_MINUTE 这几个日期时间字段。UTC 是世界时而 TZD 是当地时区。 语法: ...

    mysql数据库的基本操作语法

    注意:alter modify不支持一次修改多个列,但是Oracle支持多列修改 但是MySQL可以通过多个modify的方式完成: alter table user modify tel varchar(15) default '02087654321' first, modify name varchar(20) ...

    TianleSoftware Oracle中文学习手册

    1.2.1.1 索引的创建语法 ............................................................................................ 1.2.1.2 索引特点.......................................................................

    OCPOCA认证考试指南全册:Oracle Database 11g(1Z0-051,1Z0-052,1Z0-053)--详细书签版(第1/2部分)

    原书名: OCA/OCP Oracle Database 11g All-in-One Exam Guide with CD-ROM: Exams 1Z0-051, 1Z0-052, 1Z0-053 原出版社: McGraw-Hill Osborne Media 作者: (美)John Watson Roopesh Ramklass Bob Bryla 译者: ...

    bitmap_set_index:Oracle 数据库中的分层位图实现,用于基于集合的数据比较

    #基于集合的分层位图索引Oracle 中基于集合的分层位图索引实现,用于基于集合的操作。 该项目的目的是提供一个基于集合的运算符和索引,为基于集合的比较查询提供简单的语法和巨大的性能提升。 使用基于集合的比较的...

    小马:Pony对象关系映射器

    Pony分析了表达式的抽象语法树,并将其转换为SQL查询。 这是Pony中的查询示例: select(p for p in Product if p.name.startswith('A') and p.cost &lt;= 1000) Pony使用特定的数据库方言将查询转换为SQL。 目前...

    SQL21日自学通

    一般的语法规则 30 你的第一个查询 33 总结 37 问与答 38 校练场 38 练习 39 第三天表达式条件语句与运算 40 第四天函数对数据的进一步处理 60 目标 60 汇总函数 60 COUNT61 SUM 61 AVG 63 MAX 63 MIN 64 VARIANCE65...

    asp.net知识库

    技术基础 New Folder 多样式星期名字转换 [Design, C#] .NET关于string转换的一个小Bug Regular Expressions 完整的在.net后台执行javascript脚本...Serialize Your Deck with Positron [XML Serialization, XSD, C#]...

    vfp6.0系统免费下载

    答案: Visual FoxPro 6.0 产品中带有丰富的示例,其中有一些是针对 6.0 版特有功能的新示例。与 Visual FoxPro 以前的版本不同,这些示例将与所有 Visual Studio 示例安装在一起。您必须运行 MSDN Library 的...

    网管教程 从入门到精通软件篇.txt

    INT:中间代码,当一个源程序经过语法检查后编译产生一个可执行代码 IOF:Findit文档 IQY:Microsoft Internet查询文件 ISO:根据ISD 9660有关CD-ROM文件系统标准列出CD-ROM上的文件 ISP:X-Internet签字文件 ...

    经典SQL语句大全

    查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:...

    数据库操作语句大全(sql)

    查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:...

    sql经典语句一部分

    查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:...

    springmybatis

    MyBatis 目录(?)[-] mybatis实战教程mybatis in action之一开发环境搭建 mybatis实战教程mybatis in action之二以接口的方式编程 mybatis实战教程mybatis in action之三实现数据的增删改查 mybatis实战教程mybatis ...

Global site tag (gtag.js) - Google Analytics