pySpark.sql如何使用WHERE关键字



如何使用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
----------------------------^^^
"

你应该把它放在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()

相关内容

  • 没有找到相关文章

最新更新