SQL Presto 检查是否为 NULL 并返回默认值(NVL 模拟)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43275356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:11:37  来源:igfitidea点击:

Presto check if NULL and return default (NVL analog)

sqlprestoamazon-athena

提问by Sasha Shpota

Is there any analog of NVL in Presto DB?

Presto DB 中有 NVL 的类似物吗?

I need to check if a field is NULL and return a default value.

我需要检查一个字段是否为 NULL 并返回一个默认值。

I solve this somehow like this:

我以某种方式解决了这个问题:

SELECT
  CASE 
    WHEN my_field is null THEN 0 
    ELSE my_field 
  END
FROM my_table

But I'm curious if there is something that could simplify this code.

但是我很好奇是否有可以简化此代码的东西。

回答by David ???? Markovitz

The ISO SQL function for that is COALESCE

ISO SQL 函数是 COALESCE

coalesce(my_field,0)

https://prestodb.io/docs/current/functions/conditional.html

https://prestodb.io/docs/current/functions/conditional.html

P.S. COALESCEcan be used with multiple arguments. It will return the first (from the left) non-NULL argument, or NULL if not found.

PSCOALESCE可以与多个参数一起使用。它将返回第一个(从左侧)非 NULL 参数,如果未找到则返回 NULL。

e.g.

例如

coalesce (my_field_1,my_field_2,my_field_3,my_field_4,my_field_5)