【问题标题】:How to use Max function with Where conditions如何在 Where 条件下使用 Max 函数
【发布时间】:2019-04-23 03:59:51
【问题描述】:

我正在编写代码以从不等于其他两个大值的列中选择最大值。最大值将始终是第三个最大值。最大的两个值是占位符,(int) 年月格式 999912 和 999901。

我尝试过同时使用 Max 和 Filter,但没有成功。

val maxSurvey = s.max("SurveyMonth").filter(survey("SurveyMonth") =!= "999912" && survey("SurveyMonth") =!= "999901")

我预计当前结果是 201902。

【问题讨论】:

  • 试试s.filter($"SurveyMonth" != ""999912" && $"SurveyMonth") != "999901").max($"SurveyMonth")

标签: scala apache-spark apache-spark-sql


【解决方案1】:

您需要选择最大值,但您的代码在过滤器中也有错误,如果您需要最大值,为什么要将 SurveyMonth 与字符串进行比较?

更改后您的代码将如下所示:

val maxSurvey = s.filter('SurveyMonth =!= 999912 && 'SurveyMonth =!= 999901).select(max('SurveyMonth))

【讨论】:

  • 你可以在需要引用列时使用'column_name
猜你喜欢
  • 1970-01-01
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 2021-09-02
  • 1970-01-01
  • 2012-04-14
相关资源
最近更新 更多