【问题标题】:"expression is neither present in the group by, nor is it an aggregate function" what is wrong here?“表达式既不存在于 group by 中,也不是聚合函数”这里有什么问题?
【发布时间】:2018-12-19 15:22:37
【问题描述】:

我正在尝试在我的数据框上应用如下所示的数据透视

val pivot_company_model_vals_df =  company_model_vals_df.groupBy("company_id","id","date")
                          .pivot("code")
                          .agg( when( col("data_item_value_numeric").isNotNull,  
      first("numeric")).otherwise(first("value")) )

错误

         org.apache.spark.sql.AnalysisException: expression '`data_item_value_numeric`' is neither present in the group by, nor is it an aggregate function. 

你能帮我看看我做错了什么吗? 谢谢

【问题讨论】:

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


    【解决方案1】:

    问题固定移动first 如下.agg( first(when:

    val pivot_company_model_vals_df =  company_model_vals_df.groupBy("company_id","model_id","data_date")
                              .pivot("code")
                              .agg( first(when( col("data_item_value_numeric").isNotNull,  
          col("numeric")).otherwise(col("_string")) ) )
    

    【讨论】:

    • @SCouto,先生,我使用 agg 函数 first() 解决了这个问题,但一些数据出现错误,例如值“0.00008”为“8.0E-5”......我应该如何处理吗?
    • 8.0e-5 os 与 0.00008 完全相同。这是数据如何显示的问题。如果你想在没有指数的情况下显示它,你可以使用 f"$yourvariable%1.5f,其中 1 是小数点前的位数,小数点后的位数是 5。更多信息在这里alvinalexander.com/scala/…
    • @eliasah with col ,我如何获得列的值?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多