当两个ID在Scala中具有相同的最高价格时,以较小的ID获得最高价格 [英] Get the highest price with smaller ID when two ID have the same highest price in Scala
问题描述
我有一个数据框调用 productPrice 有列 ID 和价格,我想得到价格最高的 ID,如果两个 ID 的最高价格相同,我只得到 ID 号较小的那个.我用
I have a dataframe call productPrice have column ID and Price, I want to get the ID that had the highest price, if two ID have the same highest price, I only get the one the have the smaller ID number. I use
valhighestprice = productPrice.orderBy(asc("ID")).orderBy(desc("price")).limit(1)
但是我得到的结果不是ID较小的那个,而是ID较大的那个.我不知道我的逻辑有什么问题,有什么想法吗?
val highestprice = productPrice.orderBy(asc("ID")).orderBy(desc("price")).limit(1)
But the result I got is not the one that have the smaller ID, instead the one I got is the one the have a larger ID. I don't know what's wrong with my logic, any idea?
推荐答案
试试这个.
scala> val df = Seq((4, 30),(2,50),(3,10),(5,30),(1,50),(6,25)).toDF("id","price")
df: org.apache.spark.sql.DataFrame = [id: int, price: int]
scala> df.show
+---+-----+
| id|price|
+---+-----+
| 4| 30|
| 2| 50|
| 3| 10|
| 5| 30|
| 1| 50|
| 6| 25|
+---+-----+
scala> df.sort(desc("price"), asc("id")).show
+---+-----+
| id|price|
+---+-----+
| 1| 50|
| 2| 50|
| 4| 30|
| 5| 30|
| 6| 25|
| 3| 10|
+---+-----+
这篇关于当两个ID在Scala中具有相同的最高价格时,以较小的ID获得最高价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!