rangeBetween (ウィンドウ)
フレーム境界がstart (含む)からend (含む)まで定義されたWindowSpecを作成します。
startとendはどちらも現在の行からの相対的な位置を示します。例えば、 0 「現在の行」を意味し、 -1現在の行の1つ前を意味し、 5現在の行の5つ後を意味します。
範囲ベースの境界は、ORDER BY 式の実際の値に基づいています。オフセットは ORDER BY 式の値を変更します。たとえば、現在の ORDER BY の値が10で、下限オフセットが-3の場合、結果として得られる下限は7になります。このため、範囲ベースのフレームでは、オフセットが無制限でない限り、数値データ型の ORDER BY 式が正確に 1 つ必要になります。
構文
Window.rangeBetween(start, end)
パラメーター
パラメーター | Type | 説明 |
|---|---|---|
| int | 境界線の開始、包括的。これが |
| int | 境界線の端点を含む。これが |
戻り値
WindowSpec
注意
整数値を直接使用する代わりに、 Window.unboundedPreceding 、 Window.unboundedFollowing 、 Window.currentRowを使用して特別な境界値を指定します。
例
Python
from pyspark.sql import Window, functions as sf
df = spark.createDataFrame(
[(1, "a"), (1, "a"), (2, "a"), (1, "b"), (2, "b"), (3, "b")], ["id", "category"])
# Calculate the sum of id where the id value falls within [current id, current id + 1]
# in each category partition.
window = Window.partitionBy("category").orderBy("id").rangeBetween(Window.currentRow, 1)
df.withColumn("sum", sf.sum("id").over(window)).sort("id", "category").show()
# +---+--------+---+
# | id|category|sum|
# +---+--------+---+
# | 1| a| 4|
# | 1| a| 4|
# | 1| b| 3|
# | 2| a| 2|
# | 2| b| 5|
# | 3| b| 3|
# +---+--------+---+