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


基础教程推荐
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01