我试图用select在case when语句中运行一个查询。就像这样:
select case when cust_id in (select cust_id from tab_cust) then 'Eligible' else 'Not Eligible' end as Eligibility
from tab_product;但是,在spark中运行此查询会产生错误:
找不到表或视图: tab_cust
但是,如果在where中使用与子查询相同的select查询,则不会出现错误,但这将排除记录集,这是我不想要的。
是否有任何方法可以不使用select语句来执行此查询,以防止何时获得相同的结果?
发布于 2018-09-17 16:17:59
似乎您希望测试一个表中的值是否位于另一个表中。是真的?我用联接来完成这个任务。下面是一个火花放电的例子。
from pyspark.sql.types import *
custSchema = StructType([
StructField("cust_id", IntegerType())
])
productDF = spark.createDataFrame([(1,),
(2,),
(3,)
], schema=custSchema)
productDF.createOrReplaceTempView("tab_product")
customerDF = spark.createDataFrame([(2,),
(3,),
(4,)
], schema=custSchema)
customerDF.createOrReplaceTempView("tab_cust")
spark.sql("""
SELECT tab_product.cust_id, nvl2(tab_cust.cust_id, 'Eligible', 'Not Eligible') as Eligibility
FROM tab_product LEFT OUTER JOIN tab_cust
ON tab_product.cust_id = tab_cust.cust_id
""").show()给予:
+-------+------------+
|cust_id| Eligibility|
+-------+------------+
| 1|Not Eligible|
| 3| Eligible|
| 2| Eligible|
+-------+------------+https://stackoverflow.com/questions/52371219
复制相似问题