“当前行”与“当前行”之间的任何差异 和“0之前/之后” 在Oracle分析函数的窗口子句中?

发布于 2024-07-10 08:45:17 字数 899 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光倒影 2024-07-17 08:45:17

使用哪个并不重要。 它们是表达窗口的两种不同方式,但优化器将以相同的方式执行查询。 “当前行”一词是多个具有分析功能的数据库所共有的术语,而不仅仅是 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).

小…红帽 2024-07-17 08:45:17

我手上的 Oracle 文档(Oracle 9.2)说:

如果您指定了范围:

  • value_expr 是逻辑偏移量。 它必须是一个常量或表达式
    计算结果为正数值
    或区间文字。

这意味着您不应该真正使用 0,因为它不是正数值。 但是,显然可以在前面/后面使用 0,因为您就是这样。

The Oracle documentation that I have to hand (Oracle 9.2) says:

If you specified RANGE:

  • value_expr is a logical offset. It must be a constant or expression that
    evaluates to a positive numeric value
    or an interval literal.

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.

风流物 2024-07-17 08:45:17

这一切都与您想要实现的目标有关。
您可能想要使用 RANGE BETWEEN/ROWS BETWEEN 使用它来查找子集中的 LAST_VALUE 或比较子集中的内容。 但最肯定的是您不需要您提供的示例。

    select deptno, 
       empno,
       hiredate,
       count(*) over (partition by deptno, trunc(hiredate,'mm')) cnt_same_month
  from emp
 where deptno = 30
 order by deptno, hiredate

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.

    select deptno, 
       empno,
       hiredate,
       count(*) over (partition by deptno, trunc(hiredate,'mm')) cnt_same_month
  from emp
 where deptno = 30
 order by deptno, hiredate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文