`
hehaibo
  • 浏览: 419709 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

oracle with oracle高级查询

阅读更多
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”:





本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lpxuan151009/archive/2010/06/11/5664129.aspx
分享到:
评论

相关推荐

    oracle 高级查询

    总之,Oracle高级查询涵盖了多列子查询、EXISTS和NOT EXISTS运算符以及WITH子句等高级技术,这些都是数据库管理员和开发人员在处理复杂查询时的重要工具。通过熟练掌握这些技能,你可以更有效地管理和操作Oracle...

    oracle高级查询技巧

    以下是对"Oracle高级查询技巧"的详细阐述。 一、子查询 子查询是嵌套在其他SQL语句中的查询,用于获取主查询所需的值。它可以作为SELECT、FROM或WHERE子句的一部分。例如,你可以使用子查询来找到某个部门薪水最高...

    Oracle高级sql学习与练习

    Oracle高级SQL学习与练习涵盖了数据库编程中的一系列高级主题,旨在帮助数据库开发者和管理员提高解决复杂问题的能力。在Oracle数据库系统中,高级SQL技能是进行高效数据管理、查询优化和复杂数据处理的基础。 1. ...

    oracle advanced sql 高级SQL教程 ORACLE官方教材

    GROUP BY with ROLLUP and CUBE Operators 3-6 ROLLUP Operator 3-7 ROLLUP Operator Example 3-8 CUBE Operator 3-9 CUBE Operator: Example 3-10 GROUPING Function 3-11 GROUPING Function: Example 3-12 ...

    ORACLE SQL 高级编程.pdf (全)

    1. 高级查询技术:包括子查询、连接查询、数据分组和汇总、子查询的优化以及使用WITH子句构建复杂的查询语句。 2. 数据操纵语言(DML)高级特性:掌握高级的INSERT、UPDATE、DELETE以及MERGE语句的用法,包括DML的...

    DBExportDoc V1.0 For Oracle With Source.rar

    DBExportDoc V1.0 For Oracle With Source 是一个专为Oracle数据库设计的工具,用于高效地导出数据库的表结构。这款软件以其易用性、灵活性和实用性而受到用户的青睐,是数据库管理员和开发人员在处理Oracle数据模型...

    《Oracle SQL高级编程》书中源代码

    7. **高级查询技巧**:例如递归查询(WITH子句)、集合操作与连接的结合、动态SQL、游标和PL/SQL块等,这些都是提升SQL编程能力的重要环节。 8. **事务和并发控制**:理解事务的ACID属性(原子性、一致性、隔离性和...

    Oracle数据库-- 高级子查询chinese

    通过本章学习,您将可以: 书写多列子查询 子查询对空值的处理 在 FROM 子句中使用子查询 在SQL中使用单列子查询 相关子查询 书写相关子查询 使用子查询更新和删除数据 使用 EXISTS 和 NOT EXISTS 操作...使用 WITH 子句

    Oracle高级SQL培训与讲解.doc

    标题与描述均指向了一个主题——Oracle高级SQL培训与讲解,特别是关于WITH子句的深入...总之,掌握了WITH子句的使用,不仅能够提升SQL查询的性能,还能使代码更加整洁、高效,是Oracle高级SQL技能中不可或缺的一部分。

    Java Programming with Oracle JDBC

    #### 七、Oracle高级安全特性 - **6.1 认证**: - Oracle数据库认证机制的原理。 - 不同认证方式的优缺点。 - **6.2 数据加密**: - 加密技术在Oracle数据库中的应用。 - 实现数据加密的方法。 - **6.3 数据完整...

    Oracle9i Program with PLSQL v1

    Oracle9i Program with PLSQL是Oracle公司为开发者提供的一份关于如何在Oracle9i数据库环境中使用PLSQL编程的教程。PLSQL(Procedural Language/Structured Query Language)是Oracle数据库特有的一种结构化编程语言...

    oracle递归查询的例子

    ### Oracle 递归查询详解及实例 #### 一、引言 在数据库查询语言中,Oracle 提供了一种强大的功能——递归查询,这在其他数据库系统如 SQL Server 中是缺失的功能。递归查询允许用户执行多级关联查询,特别适用于...

    Oracle查询操作的学习笔记

    - **高级权限授予**:如果需要给予用户更多的权限,并允许该用户进一步将这些权限转授给其他用户,则可以使用`WITH GRANT OPTION`。例如,`GRANT SELECT ON scott.emp TO xiaoming WITH GRANT OPTION;` - **撤销权限...

    Oracle Database 12c Release 2 (12.2) Flex RAC with GNS On Oracle Linux 7

    ### Oracle Database 12c Release 2 (12.2) Flex RAC with GNS On Oracle Linux 7知识点总结 #### 1. Oracle Linux操作系统环境设置 Oracle Linux 7.1是安装Oracle 12c RAC(Real Application Clusters)的理想操作...

    【Oracle】树状结构查询

    本文将深入探讨如何在Oracle中执行树状结构查询,理解其背后的逻辑,以及如何利用各种高级特性来优化查询结果。 ### Oracle树状结构查询原理 树状结构查询主要依赖于`START WITH`和`CONNECT BY PRIOR`这两个关键...

    oracle查询成树状

    在Oracle数据库中,将查询结果转化为树状结构是一项高级而实用的技能,尤其适用于处理具有层级关系的数据,如产品分类、组织架构等。本篇将深入解析如何利用Oracle的特定功能,实现数据的树状展示。 ### 核心概念:...

    nacos oracle

    Oracle 的强大功能,如 ACID 事务支持、高级索引和查询优化,为 Nacos 提供了更稳定和高效的存储解决方案。然而,这也意味着更高的硬件需求和运维复杂度,因此在选择数据库时,需要根据具体业务场景和资源情况进行...

    oracle 高级复制操作步骤,花了很久才研究透彻。

    ### Oracle高级复制操作详解 #### 一、判断数据库是否支持高级复制功能 为了确定Oracle数据库是否支持高级复制功能,可以通过以下两种方式来进行检查: 1. **通过查询`v$option`视图**: ```sql SELECT value ...

    PHP-with-oracle-db.zip_oracle

    标题 "PHP-with-oracle-db.zip_oracle" 暗示了这个压缩包包含与使用PHP连接Oracle数据库相关的资源。PHP是一种流行的服务器端脚本语言,常用于构建动态网站,而Oracle则是一种强大的关系型数据库管理系统。这个...

Global site tag (gtag.js) - Google Analytics