BigQuery:检查条件表达式中是否为null

时间:2018-07-24 01:20:39

标签: sql google-bigquery

是否可以执行条件if语句将3个字段折叠为一个字段?我正在努力实现以下目标。 field1field2field3Int64是可空的。我找不到一种进行空值检查的方法,因此要检查是否为任何字段分配了正值,然后将field设置为相应的值。当下面的语法出现以下错误时:

case when field1 >= 0 then 0 end as field, 
case when field2 >= 0 then 1 end as field,
case when field3 >= 0 then 2 end as field
  

不支持结果中重复的列名。找到了   重复:字段

2 个答案:

答案 0 :(得分:2)

这是您想要的吗?

(case when field1 is not null then 0
      when field2 is not null then 1
      when field3 is not null then 2
 end) as null_check

或者,如果您要将其转换为编码字段:

concat(case when field1 is not null then 0 end,
       case when field2 is not null then 1 end,
       case when field3 is not null then 2 end,
      ) as null_check

这将在字符串中列出“ 0”,“ 1”,“ 2”,具体取决于非NULL的值。

答案 1 :(得分:1)

听起来好像您只想要一个CASE表达式,它可以基于多个条件生成多个值:

CASE WHEN field3 >= 0 THEN 2
     WHEN field2 >= 0 THEN 1
     WHEN field1 >= 0 THEN 0 END AS field

这里的逻辑是,在确定要生成的输出时,它将首先检查field3,然后检查field2,然后检查field1

相关问题