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 子句


基础教程推荐
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 在多列上分布任意行 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01