我想在 Scala 中使用窗口函数。
我有一个 CSV 文件,它是以下文件:
id;date;value1
1;63111600000;100
1;63111700000;200
1;63154800000;300
当我尝试在此数据框上应用窗口函数时,有时会起作用,有时会失败:
val df = loadCSVFile()
val tw = Window.orderBy(date).partitionBy(id).rangeBetween(-5356800000,0)
df.withColumn(value1___min_2_month, min(df.col("value1")).over(tw))
+---+-----------+--------------------+
| id| date|value1___min_2_month|
+---+-----------+--------------------+
| 1|63111600000| 100|
| 1|63111700000| 100|
| 1|63154800000| 100|
+---+-----------+--------------------+
所以它有效!但是当我尝试使用更大的数字(包含上一个示例的行)时,我得到以下结果
val tw =
Window.orderBy(date).partitionBy(id).rangeBetween(-8035200000,0) \n
df.withColumn(value1___min_3_month, min(df.col("value1")).over(tw))
+---+-----------+--------------------+
| id| date|value1___min_3_month|
+---+-----------+--------------------+
| 1|63111600000| null|
| 1|63111700000| null|
| 1|63154800000| null|
+---+-----------+--------------------+