您如何使用WHERE
关键字来获得在泰坦尼克号灾难中幸存下来的性别及其百分比?
我的代码:
spark.sql(
"SELECT Sex Where Survived=1 ,count(Sex) \
as gender_count,count(sex)*100/sum(count(sex)) over() \
as percent from titanic_table GROUP BY sex"
).show()
错误:
ParseException: " mismatched input ',' expecting <EOF>(line 1, pos 28) == SQL == SELECT Sex Where Survived=1 ,count(Sex) as gender_count,count(sex)*100/sum(count(sex)) over() as percent from titanic_table GROUP BY sex ----------------------------^^^ "
答案 0 :(得分:0)
您应该将其放在FROM
之后和GROUP BY
之前。
您的代码应为:
spark.sql("SELECT Sex, count(Sex) AS gender_count, \
100*count(sex)/sum(count(sex)) over() AS percent \
FROM titanic_table \
WHERE Survived = 1 \
GROUP BY sex").show()