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
相关文章