微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

来自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|
+---+-----------------------------------------------------+

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐