"expression is neither present in the group by, nor is it an aggregate function"这里有什么问题?

"expression is neither present in the group by, nor is it an aggregate function" what is wrong here?

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

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. 

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

修复了 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")) ) )