sparkDataframe中ifnull和iff的等价sql函数

50few1ms  于 2021-05-29  发布在  Spark
关注(0)|答案(1)|浏览(958)

我试图在sparkDataframe中使用一个雪花列(它有iffnull和iff这样的函数)。我试过合并,但不起作用。在sparkDataframe中是否有等效的函数或逻辑?
雪花sql:

SELECT P.Product_ID,
IFNULL(IFF(p1.ProductDesc='',NULL,p1.ProductDesc),
           IFNULL(IFF(p2.PrdDesc='',NULL,p2.PrdDesc),IFF(p3.Product_Desc='',NULL,p3.Product_Desc))
          ) AS Product_Description
FROM Product p
LEFT JOIN Product_table_1 p1 ON p1.Product_ID = p.Product_ID
LEFT JOIN Product_table_2 p2 ON p2.Product_ID = p.Product_ID
LEFT JOIN Product_table_3 p3 ON p3.Product_ID = p.Product_ID

我试过: coalesce(p1.ProductDesc, p2.PrdDesc, p3.Product_Desc) 但它不起作用

e5nszbig

e5nszbig1#

iiuc,您可以尝试coalesce+nullif:
nullif(expr1,expr2)-如果expr1等于expr2,则返回null,否则返回expr1。
例如(使用pyspark):

df = spark.createDataFrame([(None,"","a"),("a","","b"),("",None,"c")],["desc1", "desc2", "desc3"])

df.createOrReplaceTempView("tb")

spark.sql("select *, coalesce(nullif(desc1,''), nullif(desc2,''), nullif(desc3,'')) as desc from tb").show()       
+-----+-----+-----+----+
|desc1|desc2|desc3|desc|
+-----+-----+-----+----+
| null|     |    a|   a|
|    a|     |    b|   a|
|     | null|    c|   c|
+-----+-----+-----+----+

因此,对于您的任务,请使用以下命令:

coalesce(nullif(p1.ProductDesc,''), nullif(p2.PrdDesc,''), nullif(p3.Product_Desc,'')) as Product_Description

顺便说一句,你也可以改变一切 IFF 在原始sql中 IF .

相关问题