我刚刚开始学习SQL和Apache Spark。
我已经在Spark中导入了一个SQL表。
现在我需要找到一个基于字段的成功率,该字段需要为“yes”。
所以我需要找出总行数除以行数,其中有一个特定的字段为'yes‘
我能够找到单独的结果,但不知道如何组合这两个查询。
sqlContext.sql("select count(*) from customers")
res51: org.apache.spark.sql.DataFrame = [_c0: bigint]
sqlContext.sql("select count(*) from customers where custSub = 'yes'")
res52: org.apache.spark.sql.DataFrame = [_c0: bigint]我是否可以使用单个查询找到结果,或者在存储单个查询的结果后是否需要执行任何操作。
你能帮我解决这个问题吗?
发布于 2017-12-26 23:07:44
您可以使用条件聚合来完成此操作。
sqlContext.sql("""select count(case when custSub = 'yes' then 1 end)/count(*)
from customers
""")发布于 2017-12-26 23:12:51
下面是一个很好的小技巧,可以使用avg()获取速率
select avg(case when custSub = 'yes' then 1.0 else 0.0 end) as rate
from customers;https://stackoverflow.com/questions/47980340
复制相似问题