spark.sql( """ |select *, |if rn=1 |when gather_time=1 then 2 |end |from( |select * ,row_number() over(partition by name order by gather_time desc)rn |from gather) |""".stripMargin).show() 帮我改正确
时间: 2024-02-17 13:12:24 浏览: 139
row_number,根据多个字段过滤,partition by
以下是修改后的代码:
```
spark.sql("""
select *,
case when rn=1 and gather_time=1 then 2 else null end as new_col
from(
select *, row_number() over(partition by name order by gather_time desc) as rn
from gather
)
""").show()
```
修改说明:
1. 用`case when ... then ... else ... end`代替了原来的`if ... when ... then ... end`,因为Spark SQL中没有if语句。
2. 为`row_number()`函数的结果指定了别名`rn`。
3. 用`as`代替了原来的`|`,因为Spark SQL中不支持`|`作为换行符。
阅读全文