`

《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

    NULL 博文链接:https://caohong286.iteye.com/blog/1370976

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

    NULL 博文链接:https://caohong286.iteye.com/blog/1508023

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

    NULL 博文链接:https://caohong286.iteye.com/blog/1577086

    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-...

    Pro Oracle SQL

    Pro Oracle SQL, Second Edition 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 multi-pronged approach: learn ...

    Laravel开发-eloquent-subquery-magic

    Laravel开发-eloquent-subquery-magic 提供使用许多子查询功能(如FromSubQuery或LeftJoinSubQuery)的雄辩扩展

    eloquent-has-by-non-dependent-subquery:将has()和whereHas()约束转换为非依赖子查询

    composer require mpyw/eloquent-has-by-non-dependent-subquery 动机 假设您具有以下关系: class Post extends Model { use SoftDeletes ; public function comments (): HasMany { return $ this -> has...

    最全的oracle常用命令大全.txt

    SQL> select pro.spid from v$session ses,v$process pro where ses.sid=21 and ses.paddr=pro.addr; 说明:21是某个连接的sid数 然后用 kill 命令杀此进程号。 五、SQL*PLUS使用 a、近入SQL*Plus $sqlplus ...

    复习笔记SQL34句

    SQL语句(16) Subquery 6 SQL语句(17) UNION 6 SQL语句(18) UNION ALL 6 SQL语句(19) INTERSECT 7 SQL语句(20) MINUS 7 SQL语句(21) Concatenate 8 SQL语句(22) Substring 8 SQL语句(23) TRIM 9 SQL语句(24) Create ...

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 连接字符串

    oracle学习文档 笔记 全面 深刻 详细 通俗易懂 doc word格式 清晰 第一章 Oracle入门 一、 数据库概述 数据库(Database)是按照数据结构来组织、存储和管理数据的仓库,它产生于距今五十年前。简单来说是本身可视...

    Oracle创建视图(View)

    28-9月 -81 30 SALES 7698 BLAKE MANAGER 01-5月 -81 30 SALES 7782 CLARK MANAGER 09-6月 -81 10 ACCOUNTING 7788 SCOTT ANALYST 19-4月 -87 20 RESEARCH 7839 ...

    Oracle中的优化器如何进行评估优化

    不同的SQL结构有时具有同样的操作(例如:= ANY (subquery) and IN (subquery)),Oracle会把他们映射到一个单一的语义结构。本文将讨论优化器如何评估优化如下的情况和表达式:常量 LIKE 操作符 IN 操作符 ANY和...

    SQL语法大全

    SQL语法大全 SQL语法大全 1. ASP与Access数据库连接: dim conn,mdbfile mdbfile=server.mappath("数据库名称.mdb") set conn=server.createobject("adodb.connection") conn.open "driver={microsoft access ...

    2009达内SQL学习笔记

    export ORACLE_HOME=/oracledata/.../bin: 一、注意事项: 大小写不敏感,即不区分大小写。提倡关键字大写,便于阅读和调式。 “!”在SQL环境下执行Unix命令。 SQL语句是由简单的英语单词构成;这些英语单词...

    Oracle事例

    sql> create user juncky identified by oracle default tablespace users sql> temporary tablespace temp quota 10m on data password expire sql> [account lock|unlock] [profile profilename|default]; ...

    SQL Prompt_9.1.14.5213破解版

    SQL Prompt 9.1.14.5213 This build of SQL Prompt includes these improvements: Added support for new BULK INSERT options. SP-6939 : Code analysis no longer flags an EI003 if a non-scalar subquery is ...

    SQL语句大全 珍藏版2019-02-28

    (16) Subquery .............................. .............. .........................................9 嵌套 SELECT "栏位 1" FROM "表格" WHERE "栏位 2" [比较运算素] (SELECT "栏位 1" FROM "表格" WHERE ...

    完全剖析Microsoft SQL Server 7.0

    9. Transact-SQL程式設計 [加入我的離線書架] . 將Transact-SQL當作程式語言 . Transact-SQL程式設計構詞兒─基礎 . 總結 10. 批次、交易、預儲程序與觸發 [加入我的離線書架] . 批次(Batch) . 交易...

Global site tag (gtag.js) - Google Analytics