Iteration 迭代
(page 287)
Iteration provides another facility to implementing complex business requirements using a concise
Model SQL statement. A block of rules can be executed in a loop a certain number of times or while a
condition remains true. The syntax for the iteration is:
迭代提供了另一种功能,用精简的Model SQL 语句执行复杂的业务需求。一段规则可在一循环中执行多次或者只要条件为真(就一直执行)。迭代的句法是:
[ITERATE (n) [UNTIL <condition>] ]
( <cell_assignment> = <expression> ... )
Use the syntax ITERATE (n) to execute an expression n times. Use the expression ITERATE UNTIL
<condition> to iterate while the given condition remains true.
用句法ITERATE (n) 执行表达式n次。用表达式ITERATE UNTIL <condition>只要给定的条件保持为真就迭代。
An Example
Suppose the goal is to show five weeks of sale column values in a comma-separated list format. This
requirement is implemented in Listing 9-17.
设目标是展示五周的销售列值以逗号分隔的串格式。该需求在列表9-17执行了。
NOTE Conversion of rows to columns is termed as pivoting . Oracle database 11g introduces syntax to implement pivoting function natively. In Oracle database 10g, you could use the Model clause to implement pivoting.
注意 转换行成列术语称之为pivoting(旋转
).Oracle数据库11g引入句法可执行内在的旋转函数。在Oracle数据库10g,你可用Model子句执行旋转。
注:关于pivot和unpivot参考http://blog.csdn.net/tianlesoftware/article/details/7060306
Line 8 specifies that the rules block is to be iterated five times for each row. That’s done through
the clause rules iterate(5). In line 10, you use Iteration_number, which is a variable available within
the rules section, to access the current iteration count of the loop. Iteration_number starts with a value
of 0 for the first iteration in the loop and ends at n-1 where n is the number of loops as specified in the
iterate(n) clause. In this example, the Iteration_number variable value ranges from 0 to 4. With
Iteration_number and bit of arithmetic, you can access the prior two weeks and the next two weeks’
values using the clause CV(week)-ITERATION_NUMBER +2. The CASE statement adds a comma for each
element in the list, except for the first element.
第8行指定规则块对每行执行5次。通过子句 rules iterate(5)做到。在第10行,你用
Iteration_number,在规则段内的一个变量,用于访问循环中迭代次数。Iteration_number 对应循环的第一次迭代的起始值是0,且终止值n-1,其中n是在iterate(n) 子句中指定的循环次数。在本例中,Iteration_number
变量值域是0到4。通过Iteration_number 和一点算术,你能用子句CV(week)-ITERATION_NUMBER
+2访问之前两周和之后两周的值。CASE语句为列表的每个元素加逗号,除了第一个元素。
For example, let’s assume the current row in the process has a value of year=2001 and week=23. In
the first iteration of the loop, iteration_number will be zero, and the clause cv(week)-ITERATION_NUMBER
+2 will access the row with week=23-0+2=25. In the next iteration, week 24 will be accessed, and so on. The FOR loop is repeated for every row in the model output.
例如,我们假设处理的当前行有year=2001 and week=23的值。在循环的第一次迭代中,iteration_number
是0,且子句cv(week)-ITERATION_NUMBER +2 将访问行
week=23-0+2=25。在下一次迭代中,访问week24,如此下去。FOR循环对model输出中的每行重复。
Let’s review the output rows in Listing 9-17. For the year 2001, week 23, column Sale_list has the
following list of values: 233.7, 141.78, 22.38, 136.92, 139.28. You can see how those values are
centered on the current week. The first two come from sales column for the immediately preceding
weeks. Then you have the current week’s sales, and then the values from the following two weeks.
我们查看列表9-17的输出行。对year2001,week23,列Sale_list有下列值:233.7, 141.78, 22.38,
136.92,
139.28。你可以看出这些值是如何以当前周为中心的。前两个来至于最近前两周的sales列。接着是当前周的sales,再是接下来两周的值。
Listing 9-17. Iteration
1 select year, week,sale, sale_list
2 from sales_fact
3 where country in ('Australia') and product ='Xtend Memory'
4 model return updated rows
5 partition by (product, country)
6 dimension by (year, week)
7 measures ( cast(' ' as varchar2(50) )
sale_list, sale)
8 rules iterate (5)
(
9 sale_list [ year, week ] order by year, week =
10 sale [cv(year), CV(week)- ITERATION_NUMBER +2 ] ||
11 case when iteration_number =0 then '' else ', ' end ||
12 sale_list [cv(year) ,cv(week)]
13 )
14* order by year, week
YEAR WEEK SALE SALE_LIST
----- ---- ---------- --------------------------------------------------
2001 20 118.03 22.37, , 118.03, 233.7, 141.78
2001 21 233.70 , 118.03, 233.7, 141.78, 22.38
2001 22 141.78 118.03, 233.7, 141.78, 22.38, 136.92
2001 23 22.38 233.7, 141.78, 22.38, 136.92, 139.28
2001 24 136.92 141.78, 22.38, 136.92, 139.28,
2001 25 139.28 22.38, 136.92, 139.28, , 94.48
PRESENTV and NULLs
If a rule is accessing a non-existent row, the rule will return a null value.
Notice that in the output of
Listing 9-17, Sale_list column in the first row has two commas consecutively. The reason is that the
row for the week=19 does not exist in the data, so accessing that non- existent cell returns a null value.
You can correct this double comma issue using a function to check for cell existence using a PRESENTV
function. This function accepts three parameters and the syntax for the function is:
如果规则访问一非存在行,规则将返回null值。
注意列表9-17的输出,第一行的Sale_list列有两个逗号连在一起。原因是行week=19不存在数据,这样访问非存在行返回null值。你可以纠正这个双逗号问题,使用一个函数检查单元格的存在,用PRESENTV函数。这个函数接收3个参数,函数句法如下:
PRESENTV (cell_reference, expr1, expr2)
If cell_reference references an existing cell, then the PRESENTV function returns expr1 . If the
Cell_reference references a non-existing cell, then the second argument expr2 is returned.
In Listing
9-18, line 10 performs this existence check on the Sale column for the year and week combination
using a clause sale [cv(year), CV(week)-iteration_number + 2 ]. If the cell exists, then the function
adds the value of the cell and comma to the returned string (lines 11 to 13). If the cell does not exist, the function returns the Sale_list column without altering the string (line 14). This solution eliminates the
double comma in the Sale_ list column value.
如果cell_reference引用存在的单元格,则PRESENTV函数返回expr1。如果cell_reference引用非存在单元格,则第二
个参数expr2返回。
在列表9-18,行10对year和week的组合在Sale列上执行存在性检查,使用子句sale [cv(year),
CV(week)-iteration_number + 2
]。如果单元格存在,则函数加单元格值和逗号到返回的串(行11到13)。如果单元格不存在,则函数返回Sale_list列不改变串(行14)。这个解决方案消除Sale_list列值中的双逗号。
Listing 9-18. Iteration and presntv
1 select year, week,sale, sale_list
2 from sales_fact
3 where country in ('Australia') and product ='Xtend Memory'
4 model return updated rows
5 partition by (product, country)
6 dimension by (year, week)
7 measures ( cast(' ' as varchar2(120) ) sale_list, sale, 0 tmp)
8 rules iterate (5)
(
9 sale_list [ year, week ] order by year, week =
10 presentv (
sale [cv(year), CV(week)-iteration_number + 2 ],
11 sale [cv(year), CV(week)-iteration_number +2 ] ||
12 case when iteration_number=0 then '' else ', ' end ||
13 sale_list [cv(year) ,cv(week)] ,
14 sale_list [cv(year) ,cv(week)] )
15 )
16* order by year, week
YEAR WEEK SALE SALE_LIST
----- ---- ---------- --------------------------------------------------
2001 20 118.03 22.37, 118.03, 233.7, 141.78
2001 21 233.70 118.03, 233.7, 141.78, 22.38
2001 22 141.78 118.03, 233.7, 141.78, 22.38, 136.92
...
2001 29 116.85 94.48, 116.85, 162.91, 92.21
The PRESENTNNV function is similar to PRESENTV, but provides the additional ability to differentiate
between references to non-existent cells and null values in existing cells.
The syntax for the function
PRESENTNNV is
PRESENTNNV 函数相似于PRESENTV,但是提供而外的能力区别非存在的单元格与存在的单元格中的null值。
函数PRESENTNNV 的句法如下:
PRESENTNNV (cell_reference, expr1, expr2).
If the first argument cell_reference references an existing cell and if that cell contains non-null
value, then the first argument expr1 is returned, or else the second argument expr2 is returned. In
contrast, the PRESENTV function checks for just the existence of a cell, whereas the PRESENTNNV function checks for both the existence of a cell and Null values in that cell.
Table 9-1 lists shows the values returned from these two functions in four different cases.
如果第一个参数cell_reference引用一个存在的单元格且如果单元格包含非null值,则第一个参数expr1返回,否则第二个参数expr2
返回。相比较而言,PRESENTV函数只检查某个单元格的存在性,而PRESENTNNV函数对单元格的存在性和单元格的Null值都做检查。
表
9-1列出这两个函数在四种情况下的返回值。
Table 9-1. Presentv and presentnnv Comparison
Cell exists? |
Null? |
Presentv |
Presentnnv |
Y |
Not null |
expr1 |
expr1 |
Y |
Null |
expr1 |
expr2 |
N |
Not null |
expr2 |
expr2 |
N |
Null |
expr2 |
expr2 |
分享到:
相关推荐
本章“《Pro Oracle SQL》CHAPTER 9 The Model Clause”重点讲解了Model子句的使用,特别是其在聚合操作中的应用。 Model子句的基本结构允许用户指定一个数据区域(即模型区域),在这个区域内对数据进行计算和操作...
《Pro Oracle SQL》一书的第9章深入探讨了Oracle数据库中的"Model"子句,这一章节重点关注如何使用Model子句来更新数据行。在Oracle SQL中,Model子句是一种强大的功能,允许进行复杂的行处理和模拟迭代计算,通常...
《Pro Oracle SQL》一书的第9章深入探讨了Oracle数据库中的“Model”子句,这一章节重点关注了9.5节——评估顺序。在Oracle SQL中,Model子句是一种高级的行处理工具,用于进行复杂的行计算和模拟迭代过程,比如解决...
《Pro Oracle SQL》一书中的第9章详细探讨了Oracle SQL中的“模型”子句,这一章重点关注了位置引用和符号引用的概念。在Oracle数据库中,模型子句是一种强大的工具,用于处理复杂的行列操作,例如数据建模、数据...
《Pro Oracle SQL》是Oracle数据库查询的一本权威指南,其中第9章主要讲解了如何使用Model子句进行行间引用,这是一个高级SQL特性,用于处理复杂的行与行之间的计算和逻辑操作。9.2章节专注于Inter-Row Referencing...
《Pro Oracle SQL》一书的第9章,重点关注了使用"Model"子句进行性能调优的方法。在Oracle数据库中,Model子句是一种强大的功能,它允许数据建模和复杂的计算,尤其适用于解决多步骤计算问题,如模拟、预测和序列...
《Pro Oracle SQL》一书的第七章深入探讨了高级分组技术,特别是关于`HAVING`子句的部分。`HAVING`子句在SQL查询中扮演着至关重要的角色,它用于在聚合函数的结果集上设置条件,这与`WHERE`子句有所不同。`WHERE`...
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 ...
Walking the Tree: From the Top Down 5-9 Ranking Rows with the LEVEL Pseudocolumn 5-10 Formatting Hierarchical Reports Using LEVEL and LPAD 5-11 Pruning Branches 5-13 Summary 5-14 Practice 5 Overview 5...
### Oracle SQL 优化与调优技术详解:深入理解SQL提示 #### 一、SQL提示的定义及作用 在Oracle数据库的SQL语句中,SQL提示(Hints)是一种用于指导优化器选择特定执行计划的特殊注释语法。这些提示能够帮助数据库...
Clause-view个人中心、设置常用的itemview
资源分类:Python库 所属语言:Python 资源全名:clause-1.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Oracle和SqlServer语法区别 Oracle和SqlServer是两种流行的关系型数据库管理系统,它们之间存在着一些语法区别。了解这些区别对于开发者来说非常重要,因为它可以帮助他们更好地迁移到新的数据库管理系统。下面将...
### ORACLE和SQL Server的语法区别 #### 一、概述 本文主要介绍Oracle与SQL Server在SQL语言层面的异同之处,重点在于Transact-SQL(T-SQL)与PL/SQL之间的区别,并提供了一些迁移策略。对于希望将现有的Oracle...
Oracle SQL查考手册是一部关于Oracle数据库查询语言的重要参考资料,它涵盖了运算符、表达式、条件、函数以及常见的SQL DDL(Data Definition Language)和Clause等内容。这篇总结将深入解析这些核心概念,帮助读者...
Chatopera 语义理解系统:机器学习,聊天机器人,意图识别clause-osc.zip