Spark SQL Row_number() PartitionBy Sort Desc

后端 未结 3 1024
刺人心
刺人心 2021-02-01 15:50

I\'ve successfully create a row_number() partitionBy by in Spark using Window, but would like to sort this by descending, instead of the default ascend

3条回答
  •  一向
    一向 (楼主)
    2021-02-01 16:37

    desc should be applied on a column not a window definition. You can use either a method on a column:

    from pyspark.sql.functions import col, row_number
    from pyspark.sql.window import Window
    
    F.row_number().over(
        Window.partitionBy("driver").orderBy(col("unit_count").desc())
    )
    

    or a standalone function:

    from pyspark.sql.functions import desc
    from pyspark.sql.window import Window
    
    F.row_number().over(
        Window.partitionBy("driver").orderBy(desc("unit_count"))
    )
    

提交回复
热议问题