[20180917]关于分析函数的range与rows的区别.txt --//这几天看文档<Oracle SQL Revealed.pdf>,主要想了解学习分析函数这方面内容. --//遇到一个问题,P99. SCOTT@test01p> @ ver1 PORT_STRING VERSION BANNER CON_ID ------------------------------ -------------- -------------------------------------------------------------------------------- ---------- IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0 with t as (select rownum id, column_value value from table(sys.odcinumberlist (1,2,3,4.5,4.6,7,10))) select t.*, last_value(value) over (order by value range between unbounded preceding and 1 preceding) l1, last_value(value) over (order by value rows between unbounded preceding and 1 preceding) l2 from t; ID VALUE L1 L2 ---------- ---------- ---------- ---------- 1 1 2 2 1 1 3 3 2 2 4 4.5 3 3 5 4.6 3 4.5 6 7 4.6 4.6 7 10 7 7 7 rows selected. --//不明白为什么id=5,L1输出是3,L2输出是4.5. --//我仔细看了问题的关键理解窗口函数里面range与rows的含义. --//看了一些文档,才明白,实际上还真不好理解.做一个笔记: --//range是逻辑窗口,是指定当前行对应值的范围取值(注意包括当前行),列数不固定,只要行值在范围内,对应列都包含在内,注意 --//理解指的是取值范围.对应字段是value. --//按照例子:range between unbounded preceding and 1 preceding, 对应id=5那行,values的取值范围是1到4.6-1,实际上就是1到3.6 --//这样id=5那行L1的输出=3. --//rows是物理窗口,即根据order by 子句排序后,取的前N行及后N行的数据计算(与当前行的值无关,只与排序后的行号相关), --//对于rows between unbounded preceding and 1 preceding,对应id=5那行,行号就是1到4(注意这里值行号),这样L2的输出就是5. --//有点不好理解如果写成如下: with t as (select rownum id, column_value value from table(sys.odcinumberlist (1,2,3,4.5,4.6,7,10))) select t.*, last_value(value) over (order by value range between unbounded preceding and current row) l1, last_value(value) over (order by value rows between unbounded preceding and 1 preceding) l2 from t; ID VALUE L1 L2 ---------- ---------- ---------- ---------- 1 1 1 2 2 2 1 3 3 3 2 4 4.5 4.5 3 5 4.6 4.6 4.5 6 7 7 4.6 7 10 10 7 7 rows selected. --//如果1 preceding换成current row,value的取值范围是1到4.6(id=5). --//另外书中还介绍: "partition by part" means that we apply an analytic function for each part independently. If it's omitted, then the whole recordset is treated as one partition. Without an "order by" clause, window for each row covers all the rows for the current partition so the result is the same for all rows. With an "order by" clause, window for each row covers all rows from the beginning of the partition to the current row. This can be adjusted by specifying a windowing clause after "order by" while the default behavior is "range between unbounded preceding and current row" (or simply "range unbounded preceding") when "order by" is specified; otherwise it's "range between unbounded preceding and unbounded following." --//里面partition by ,order by 作用范围缺省从开始到当前行. --//缺省就是,这里并不指rows: --//缺省是 range between unbounded preceding and current row --//也可以写 range unbounded preceding --//range between unbounded preceding and unbounded following --//随手看了该书后面的一些例子好难,还是放弃....
[20180917]关于分析函数的range与rows的区别.txt
来源:这里教程网
时间:2026-03-03 11:59:21
作者:
编辑推荐:
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- 沃趣微讲堂 | Oracle集群技术(二):GI与Oracle RAC
沃趣微讲堂 | Oracle集群技术(二):GI与Oracle RAC
26-03-03 - set unused 列恢复
set unused 列恢复
26-03-03 - Word页码字体大小如何设置?Word页码字体大小设置方法
Word页码字体大小如何设置?Word页码字体大小设置方法
26-03-03 - 10大性能监控指令
10大性能监控指令
26-03-03 - SUSE安装oracle client客户端58%出现卡死现象
SUSE安装oracle client客户端58%出现卡死现象
26-03-03 - GoldenGate 自动化初始数据
GoldenGate 自动化初始数据
26-03-03 - word2010中如何实现双面打印文档
word2010中如何实现双面打印文档
26-03-03 - powermt 命令简介
powermt 命令简介
26-03-03 - ORACLE 11G dgbroker异常之ORA-16820&ORA-16825&ORA-12541
- expdp遇到ORA-31655错误
expdp遇到ORA-31655错误
26-03-03
