我想知道什么是Hive中的NULLIF
的替代品?我正在使用COALESCE,但它不符合我的要求。我的查询语句如下:
COALESCE(A,B,C) AS D
COALESCE
将首先返回NOT NULL值。但是我的A / B / C包含空值,因此COALESCE没有将该值赋给D,因为它将空白视为NOT NULL。但我希望将正确的值分配给D.
在SQL中,我可以使用COALESCE(NULLIF(A,'')......)
,因此它也会检查空白。我试过CASE但它不起作用。
只需使用case
:
select (case when A is null or A = '' then . . . end)
这是标准的SQL,因此它可以在Hive和其他地方使用。
对于您的特定问题:
select (case when A is not null and A <> '' then A
when B is not null and B <> '' then B
else C
end)
你实际上可以缩短到:
select (case when A <> '' then A
when B <> '' then B
else C
end)
因为NULL
值无法比较。我会使用这个版本,但通常人们学习SQL更喜欢使用not null
比较更明确的版本。
只需使用如下的案例语法:
select
coalesce(
case when A = '' then NULL else A end
,case when B = '' then NULL else B end
,case when C = '' then NULL else C end
) as D
from myTable
希望可以解决你的问题。谢谢..