当两个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 编号较小的那个。我用
val highestprice = productPrice.orderBy(asc("ID")).orderBy(desc("price")).limit(1)
但是我得到的结果不是ID小的,而是ID大的。我不知道我的逻辑有什么问题,知道吗?
试试这个。
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|
+---+-----+
使用 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)
我有一个调用 productPrice 的数据框,它有列 ID 和价格,我想获取价格最高的 ID,如果两个 ID 的最高价格相同,我只获取 ID 编号较小的那个。我用
val highestprice = productPrice.orderBy(asc("ID")).orderBy(desc("price")).limit(1)
但是我得到的结果不是ID小的,而是ID大的。我不知道我的逻辑有什么问题,知道吗?
试试这个。
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|
+---+-----+
使用 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)