簡體   English   中英

Hive中NULLIF的替換是什么?

[英]What is the replacement of NULLIF in Hive?

我想知道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比較。

從 2.3.0 開始,NULLIF 在 Hive 中可用。

您可以在他們的條件函數列表中看到它。

只需使用如下的案例語法:

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

希望可以解決你的問題。 謝謝..

另一個特定於HiveQL的選項是:

create temporary macro nullify(s string) if(s = '', null, s);
--
select coalesce(nullify(A), nullify(B), nullify(C)) as D ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM