OVER clause in Oracle(Oracle 中的 OVER 子句)
问题描述
Oracle 中的OVER 子句是什么意思?
What is the meaning of the OVER clause in Oracle?
推荐答案
OVER 子句指定了分区、排序和窗口over which"解析函数运行.
The OVER clause specifies the partitioning, ordering and window "over which" the analytic function operates.
示例 1:计算移动平均线
AVG(amt) OVER (ORDER BY date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)
date amt avg_amt
===== ==== =======
1-Jan 10.0 10.5
2-Jan 11.0 17.0
3-Jan 30.0 17.0
4-Jan 10.0 18.0
5-Jan 14.0 12.0
它在按日期排序的行上的移动窗口(3 行宽)上运行.
It operates over a moving window (3 rows wide) over the rows, ordered by date.
示例 2:计算运行余额
SUM(amt) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
date amt sum_amt
===== ==== =======
1-Jan 10.0 10.0
2-Jan 11.0 21.0
3-Jan 30.0 51.0
4-Jan 10.0 61.0
5-Jan 14.0 75.0
它在一个包含当前行和所有先前行的窗口上运行.
It operates over a window that includes the current row and all prior rows.
注意:对于带有 OVER 子句指定排序 ORDER 的聚合,默认窗口是 UNBOUNDED PRECEDING 到 CURRENT ROW,所以上面的表达式可以简化为,结果相同:
Note: for an aggregate with an OVER clause specifying a sort ORDER, the default window is UNBOUNDED PRECEDING to CURRENT ROW, so the above expression may be simplified to, with the same result:
SUM(amt) OVER (ORDER BY date)
示例 3:计算每个组内的最大值
MAX(amt) OVER (PARTITION BY dept)
dept amt max_amt
==== ==== =======
ACCT 5.0 7.0
ACCT 7.0 7.0
ACCT 6.0 7.0
MRKT 10.0 11.0
MRKT 11.0 11.0
SLES 2.0 2.0
它在一个包含特定部门的所有行的窗口上运行.
It operates over a window that includes all rows for a particular dept.
SQL Fiddle:http://sqlfiddle.com/#!4/9eecb7d/122
SQL Fiddle: http://sqlfiddle.com/#!4/9eecb7d/122
这篇关于Oracle 中的 OVER 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 中的 OVER 子句
基础教程推荐
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
