`

《Pro Oracle SQL》Chapter 9 -- 9.11 Subquery Factoring

阅读更多

Subquery Factoring    子查询分解     (page 303)
    In a business setting, requirements are complex and multiple levels of aggregation are often needed.
When writing complex queries, you can often combine subquery factoring with the Model clause to
prevent a SQL statement from becoming unmanageably complex.
    在实际的业务环境中,需求是复杂的而多层次聚合经常是必须的。当写复杂查询,通常把子查询分解与Model子句组合,防止SQL语句变得不可管理的复杂。
    Listing 9-34 provides one such example. Two Model clauses are coded in the same SQL statement.
The first Model clause is embedded within a view that is the result of a subquery being factored into the
WITH clause. The main query uses that view to pivot the value of the Sale column from the prior year.
The output shows that prior week sales are pivoted into the current week’s row.
    列表9-34提供了这样一个例子。两个Model子句在写在同一个SQL语句中。第一个Model子句嵌入一视图中,是一子查询被分解入WITH子句结果中。主查询用视图旋转来自前一年的Sale列值。输出显示前一周的销售额旋转进了当前周的行中。
Listing 9-34.  More Indexing with SQL Access in Mind    多索引用于SQL访问的思考
with t1 as (
  select  product, country, year, week, inventory, sale, receipts
  from sales_fact sf
  where country in ('Australia') and product='Xtend Memory'
  model return updated rows
  partition by (product, country)
  dimension by (year, week)
  measures ( 0 inventory , sale, receipts)
  rules automatic order(
       inventory [year, week ] order by year, week =
                                 nvl(inventory [cv(year), cv(week)-1 ] ,0)
                                  - sale[cv(year), cv(week) ] +
                                  + receipts [cv(year), cv(week) ]
   )
)
select product, country, year, week , inventory, sale,receipts,  prev_sale
from t1
model return updated rows
partition by (product, country)
dimension by (year, week)
measures (inventory, sale, receipts,0 prev_sale)
rules sequential order (
  prev_sale [ year, week ] order by year, week =
    nvl (sale [ cv(year) -1, cv(week)],0 )
)
order by 1,2,3,4
/
 
PRODUCT      COUNTRY      YEAR WEEK  INVENTORY       SALE   RECEIPTS  PREV_SALE
------------ ---------- ------ ---- ---------- ---------- ---------- ----------
Xtend Memory Australia    1998   50     11.504      28.76     40.264          0
...
Xtend Memory Australia    2000   50     12.714      21.19     25.428          0
...
Xtend Memory Australia    2001   50     11.775      23.14     32.396      21.19

 

 

注: Factoring 在数学上的意思是“因式分解” , 以后均简称“分解”。pivot本意是“绕...旋转”,但是很多书将这里的PIVOT翻译成“转置”,使人同线性代数中的转置(transpose)混淆,实际上两个不同的概念。本人喜欢直接称之为"旋转"。

 

Summary    总结
     I can’t stress enough the importance of thinking in terms of sets when writing SQL statements. Many
SQL statements can be rewritten concisely using the Model clause discussed in this chapter. As an
added bonus, rewritten queries such as Model or analytic functions can perform much better than
traditional SQL statements. A combination of subquery factoring, Model, and analytic functions
features can be used effectively to implement complex requirements. 
    必须强调依据集合思想编写SQL语句的重要性 。许多SQL语句能用本章讨论的Model子句精简的重写。作为一个额外的好处,用Model或者功能函数重写的查询将比传统SQL语句执行的更好。与子查询因式分解结合,Model和分析函数的特性能高效的用于执行复杂需求。

1
12
分享到:
评论

相关推荐

    《Pro Oracle SQL》CHAPTER2--2.8 Subquery Unnesting

    《Pro Oracle SQL》是Oracle数据库查询的一本权威指南,其中第二章主要讲解SQL查询的高级技巧。在2.8节“子查询展开(Subquery Unnesting)”中,我们将会深入探讨Oracle SQL中一种优化查询性能的重要策略。这一部分...

    《Pro Oracle SQL》Chapter 10 Subquery Factoring --10.1 Standard Usage

    《Pro Oracle SQL》第10章"Subquery Factoring"深入探讨了这一主题,尤其是10.1节“Standard Usage”部分,讲解了子查询在实际应用中的常见用法。在本章节中,作者可能详细阐述了如何利用子查询来优化SQL语句,提高...

    《Pro Oracle SQL》Chapter 10.2.5 Applying Subquery Factoring to PL/SQL

    这一概念在《Pro Oracle SQL》的第10章第2.5节中得到了深入的探讨,该章节专门讲解如何将Subquery Factoring应用到PL/SQL中。在本篇文章中,我们将深入理解这一强大的特性,并结合提供的SQL脚本文件进行解析。 首先...

    Pro Oracle SQL

    Pro Oracle SQL unlocks the power of SQL in the Oracle Database—one of the most potent SQL implementations on the market today. To master it requires a three-pronged approach: learn the language ...

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

    Nonpairwise Comparison Subquery 4-9 Using a Subquery in the FROM Clause 4-10 Scalar Subquery Expressions 4-11 Scalar Subqueries: Examples 4-12 Correlated Subqueries 4-14 Using Correlated Subqueries 4-...

    oracle-function--api.zip_oracle_oracle API_oracle sql api

    本资料“oracle-function--api.zip”包含了Oracle数据库中的一些核心API和SQL函数,特别是针对字符串、日期和数字类型的处理,对于Oracle开发人员来说极具参考价值。 一、Oracle SQL API Oracle SQL API是Oracle...

    OracleSQL实例-复杂查询语句的使用实例.pdf

    Oracle SQL 是一种强大的数据库查询语言,用于从关系型数据库中检索、更新和管理数据。本实例主要探讨了如何使用复杂的查询语句,包括子查询和外连接,来处理多表查询。 1. 子查询(Subquery) 子查询是嵌套在其他...

    Oracle SQL Recipes A Problem-Solution Approach

    ### Oracle SQL Recipes: A Problem-Solution Approach #### 核心知识点概述 《Oracle SQL Recipes: A Problem-Solution Approach》是一本面向Oracle SQL开发者和技术人员的专业书籍。本书通过一系列实际问题和...

    Laravel开发-eloquent-subquery-magic

    子查询是在一个SQL查询中嵌套另一个完整的SQL查询,它可以作为选择、比较或者联接的数据源。在Laravel的Eloquent中,可以使用`DB::raw()`方法创建子查询。Eloquent Subquery Magic则提供了更高级的API,使得子查询...

    Oracle SQL Reference9i+10g

    3. **Recursive Subquery Factoring (WITH子句)**:提供了一种新的查询结构,用于编写递归查询,处理层次关系数据。 4. **分区增强**:如范围分区中的区间分区(Interval Partitioning),可以自动处理新数据的分区...

    Oracle数据库学习日记-实用性最强的Oracle学习总结.docx

    **1.1.2 PL/SQL连接数据库配置,Oracle客户端的配置文件** - **用途:** 配置客户端环境,以便能够连接到Oracle数据库。 - **配置文件:** `tnsnames.ora` 和 `sqlnet.ora` **1.1.3 配置Oracle数据库监听Assistant...

    SQL-grammer-collection.zip_sql语句collection

    10. **SQL标准与方言**: ANSI SQL是通用标准,但各数据库系统如MySQL、Oracle、SQL Server等有其特定的语法和功能。 这份“SQL-grammer-collection.doc”文档将这些知识点逐一阐述,是学习和提升SQL技能的宝贵资源...

    java笔记\Oracle里sql不能用limit的处理

    Java笔记--Oracle中SQL不能用Limit的处理 在Java开发中,我们经常需要对数据库进行分页查询,但是Oracle数据库不支持Limit关键字,那么如何在Oracle中实现分页查询呢?下面,我们将详细介绍Oracle中SQL不能用Limit...

    基于Oracle的SQL优化典型案例分

    - 使用“Exists”代替“In”子查询,或利用“Subquery Factoring”(WITH子句)来重用子查询结果。 6. **分组和排序优化** - `GROUP BY`和`ORDER BY`操作可能导致临时表和额外的排序,优化这些操作可以节省大量...

    Apress.Pro.Oracle.SQL

    《专业Oracle SQL》一书由Karen Morton、Kerry Osborne、Robyn Sands、Riyaj Shamsudeen和Jared Still联合编写,旨在深入探讨Oracle数据库中的SQL语言及其支持功能,帮助读者全面掌握并充分利用SQL的强大能力。...

    sql基本练习-详细

    SQL(Structured Query Language)是用于管理和处理关系数据库的标准语言,其功能强大且广泛应用于各种数据库系统,如MySQL、Oracle、SQL Server等。本资料包主要包含四个方面的内容,分别是SQL基本查询操作、多表...

    SQL语句教程--教你写好SQL语句

    ### SQL语句教程知识点详解 #### 一、SELECT语句 **定义与作用:** - `SELECT` 是SQL中最常用的语句之一,主要用于从数据库表中检索数据。 - **基本语法**: ```sql SELECT column_name FROM table_name; ``` - ...

    Oracle-SQL-练习题及标准答案.doc

    Oracle SQL 是一种强大的数据库查询和管理语言,广泛用于企业级数据管理和分析。在这个文档中,NiCoBee 提供了一组 Oracle SQL 的练习题,旨在帮助学习者掌握各种查询技术。我们将深入探讨其中的一些关键概念。 1. ...

    oracle-interview-questions

    ### Oracle Interview Questions详解 ...以上内容涵盖了Oracle面试中常见的问题及其解答,这些问题涵盖了从基本的SQL操作到更高级的数据库管理技巧,对于准备Oracle面试的人来说是非常有用的资源。

Global site tag (gtag.js) - Google Analytics