当两个ID在Scala中具有相同的最高价格时,以较小的ID获得最高价格 [英] Get the highest price with smaller ID when two ID have the same highest price in Scala
问题描述
我有一个数据框调用productPrice,它具有列ID和Price,我想获得具有最高价格的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
val highestprice = 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屋!