如何处理无法识别为NULL的NULL

问题描述

我正在处理从雪花进入dbt的数据,其中某些NULL未被识别为NULL。我很高兴用案例陈述来解决这些问题,但是如何识别它们呢?

是否有设置或我们需要切换以解决这些问题的东西?

这是我的查询

select distinct call_direction,case 
        when call_direction is null then true
        else false
      end as flag
 from {{ ref('fct_phone_logs')}}

输出

enter image description here

解决方法

您必须将其中一个作为SQL NULL,而另一个则没有。如果不是SQL NULL,则不会将其标识为NULL。请运行以下内容,您将知道我要说的话。

CREATE TABLE NULL_CHECK(VALUE VARCHAR);

SELECT * FROM NULL_CHECK;
INSERT INTO NULL_CHECK VALUES (NULL);  -- This is SQL null
INSERT INTO NULL_CHECK VALUES ('NULL'); -- This is not

SELECT * FROM NULL_CHECK WHERE VALUE is null;

尝试按以下方式查询。我相信它会起作用

select distinct VALUE,case 
        when VALUE is null OR EQUAL_NULL(VALUE,'NULL') then true
        else false
      end as flag
      from NULL_CHECK;
,

我相信问题出在您的distinct关键字上。

尝试以下方法:

select
  call_direction,case when call_direction is null then true else false end as flag
from {{ ref('fct_phone_logs')}}
group by 1,2
,

dbt将空白文本(在文本值:“”的意义上)视为与实际null截然不同的类型“ null value”。

感谢@Mike Walton建议在雪花UI中运行此查询。

enter image description here

因此,我将查询更改为以下内容:

select distinct call_direction,case 
        when call_direction is null then 'true'
        when call_direction = 'NULL' then 'text null'
        when call_direction = '' then 'text blank'
        when EQUAL_NULL(call_direction,'NULL') then 'not SQL null'
        else 'false'
      end as flag
 from {{ ref('fct_phone_logs')}}

现在我可以识别所有状态。