Get the highest price with smaller ID when two ID have the same highest price in Scala

三世轮回 提交于 2019-12-25 19:00:02

问题


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) 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?


回答1:


Try this.

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|
+---+-----+



回答2:


Approaching the same problem using Spark SQL:

val df = Seq((4, 30),(2,50),(3,10),(5,30),(1,50),(6,25)).toDF("id","price")

df.createOrReplaceTempView("prices")

--

%sql
SELECT id, price
FROM prices
ORDER BY price DESC, id ASC
LIMIT(1)


来源:https://stackoverflow.com/questions/55133532/get-the-highest-price-with-smaller-id-when-two-id-have-the-same-highest-price-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!