如何处理未识别为 NULL 的 NULL

What to do about NULLs that are not recognised as NULL

我正在处理从 snowflake 进入 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')}}

和输出

我认为问题出在您的 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

您必须将其中一个设为 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;

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

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

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

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')}}

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