“当前行”与“当前行”之间的任何差异 和“0之前/之后” 在Oracle分析函数的窗口子句中?
Oracle 的一些分析函数允许使用 窗口子句 使用“无界前/后”、“当前行”或“value_expr 前/后”等关键字指定当前分区的子集,其中 value_expr 是相对于当前行或值的物理或逻辑偏移量(取决于您是否分别指定了 ROW 或 RANGE)。
这是一个使用 scott/tiger 的示例,显示第 30 部门的员工,以及在他们之前雇用的部门中的员工人数(包括他们自己):
select deptno,
empno,
hiredate,
count(*) over (partition by deptno
order by hiredate
range between unbounded preceding and current row) cnt_hired_before1,
count(*) over (partition by deptno
order by hiredate
range between unbounded preceding and 0 preceding) cnt_hired_before2
from emp
where deptno = 30
order by deptno, hiredate;
...任何人都可以提供一个示例或文档,其中“当前行”是与“0 前/后”不同? 对我来说这就像语法糖......
Some of Oracle's analytic functions allow for a windowing clause to specify a subset of the current partition, using keywords like "unbounded preceding/following", "current row", or "value_expr preceding/following" where value_expr is a physical or logical offset from the current row or value (depending on whether you have specified ROW or RANGE, respectively).
Here is an example using scott/tiger that displays employees in dept 30, and a count of the number of employees in their dept hired before them (including themselves):
select deptno,
empno,
hiredate,
count(*) over (partition by deptno
order by hiredate
range between unbounded preceding and current row) cnt_hired_before1,
count(*) over (partition by deptno
order by hiredate
range between unbounded preceding and 0 preceding) cnt_hired_before2
from emp
where deptno = 30
order by deptno, hiredate;
...can anyone provide an example or documentation where "current row" is different than "0 preceding/following"? It just seems like syntactic sugar to me...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用哪个并不重要。 它们是表达窗口的两种不同方式,但优化器将以相同的方式执行查询。 “当前行”一词是多个具有分析功能的数据库所共有的术语,而不仅仅是 Oracle。 这更多的是风格上的差异,就像有些人更喜欢 count(*) 而不是 count(1) 一样。
It doesn't really matter which you use. They are two different ways of expressing the windowing, but the optimizer will perform the query the same way. The term "current row" is one that is common to multiple databases with analytic functions, not just Oracle. It's more of a stylistic difference, in the same way that some people prefer count(*) over count(1).
我手上的 Oracle 文档(Oracle 9.2)说:
这意味着您不应该真正使用 0,因为它不是正数值。 但是,显然可以在前面/后面使用 0,因为您就是这样。
The Oracle documentation that I have to hand (Oracle 9.2) says:
This implies that you shouldn't really be using 0 since it isn't a positive numeric value. But, obviously it is possible to use 0 preceding/following since you are.
这一切都与您想要实现的目标有关。
您可能想要使用 RANGE BETWEEN/ROWS BETWEEN 使用它来查找子集中的 LAST_VALUE 或比较子集中的内容。 但最肯定的是您不需要您提供的示例。
It is all about what you're trying to accomplish.
You may want to use RANGE BETWEEN/ROWS BETWEEN use it to find LAST_VALUE within the sub-set or compare things within a sub-set. But most certainly you don't need for the example you provided.