加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql – rowsBetween和rangeBetween之间有什么区别?

发布时间:2020-12-12 07:01:44 所属栏目:MsSql教程 来源:网络整理
导读:来自PySpark docs rangeBetween : rangeBetween(start,end) Defines the frame boundaries,from start (inclusive) to end (inclusive). Both start and end are relative from the current row. For example,“0” means “current row”,while “-1” mean
来自PySpark docs rangeBetween

rangeBetween(start,end)

Defines the frame boundaries,from start (inclusive) to end (inclusive).

Both start and end are relative from the current row. For example,“0” means “current row”,while “-1” means one off before the current row,and “5” means the five off after the current row.

Parameters:

  • start – boundary start,inclusive. The frame is unbounded if this is -sys.maxsize (or lower).
  • end – boundary end,inclusive. The frame is unbounded if this is sys.maxsize (or higher).
    New in version 1.4.

rowsBetween

rowsBetween(start,from start (inclusive) to end (inclusive).

Both start and end are relative positions from the current row. For example,while “-1” means the row before the current row,and “5” means the fifth row after the current row.

Parameters:

  • start – boundary start,inclusive. The frame is unbounded if this is sys.maxsize (or higher).
    New in version 1.4.

对于rangeBetween例如,“1 off”与“1行”有何不同?

解决方法

很简单:

> ROWS BETWEEN并不关心确切的值.它只关心计算帧时的行顺序.
> RANGE BETWEEN在计算帧时考虑值.

让我们使用两个窗口定义的示例:

>在前2行和当前行之间排序x行
>在2前进和当前行之间按x排序

和数据为

+---+
|  x|
+---+
| 10|
| 20|
| 30|
| 31|
+---+

假设当前行是第一个窗口的值为31的行,将包含以下行(当前一个,前两个):

+---+----------------------------------------------------+
|  x|ORDER BY x ROWS BETWEEN 2  PRECEDING AND CURRENT ROW|
+---+----------------------------------------------------+
| 10|                                               false|
| 20|                                                true|
| 30|                                                true|
| 31|                                                true|
+---+----------------------------------------------------+

并且对于下面的第二个(当前的一个,以及前面的所有,其中x> = 31 – 2):

+---+-----------------------------------------------------+
|  x|ORDER BY x RANGE BETWEEN 2  PRECEDING AND CURRENT ROW|
+---+-----------------------------------------------------+
| 10|                                                false|
| 20|                                                false|
| 30|                                                 true|
| 31|                                                 true|
+---+-----------------------------------------------------+

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读