2017-07-15 70 views
1
DF.groupBy("id") 
    .agg(
    sum((when(upper($"col_name") === "text", 1) 
    .otherwise(0))) 
    .alias("df_count") 
    .when($"df_count"> 1, 1) 
    .otherwise(0) 
) 

我可以對名爲別名的列進行聚合嗎? ,即如果總和大於1,則返回1否則0彙總衍生列火花

在此先感謝。

回答

0

我想你可以換另一個when.otherwise圍繞sum結果:

val df = Seq((1, "a"), (1, "a"), (2, "b"), (3, "a")).toDF("id", "col_name") 
df.show 
+---+--------+ 
| id|col_name| 
+---+--------+ 
| 1|  a| 
| 1|  a| 
| 2|  b| 
| 3|  a| 
+---+--------+ 

df.groupBy("id").agg(
    sum(when(upper($"col_name") === "A", 1).otherwise(0)).alias("df_count") 
).show() 
+---+--------+ 
| id|df_count| 
+---+--------+ 
| 1|  2| 
| 3|  1| 
| 2|  0| 
+---+--------+ 


df.groupBy("id").agg(
    when(sum(when(upper($"col_name")==="A", 1).otherwise(0)) > 1, 1).otherwise(0).alias("df_count") 
).show() 
+---+--------+ 
| id|df_count| 
+---+--------+ 
| 1|  1| 
| 3|  0| 
| 2|  0| 
+---+--------+ 
+1

完美,謝謝Psidom – Babu