【问题标题】:SQL presto - cross join unnest null valueSQL presto - 交叉连接未嵌套空值
【发布时间】:2021-07-28 00:44:55
【问题描述】:

我有不同大小的数组,我希望数组中的每个值都位于不同的行中。 为此,我使用了交叉连接 unnest。但是它正在工作,它正在删除空数组。

所以,当我这样做时,我的列 ID 包含不同的数组,有些是空值

select *
    from table 
    cross join unnest (t.id) as t(order_id)
    where length(order_id) = 5  or order_id is NULL
 

我只得到以下结果

ID order_id
23deo jfr32 6582w 23deo
23deo jfr32 6582w jfr32
23deo jfr32 6582w 6582w

我想要

ID order_id
23deo jfr32 6582w 23deo
23deo jfr32 6582w jfr32
23deo jfr32 6582w 6582w
null null

如果有人知道如何取消嵌套空值,将不胜感激。我一直在网上寻找,我看到我们可以包含一个 WITH ORDINALITY 子句,但我不知道它是如何工作的。

【问题讨论】:

    标签: sql null presto unnest trino


    【解决方案1】:

    使用LEFT JOIN UNNEST 而不是CROSS JOIN UNNEST。 在Presto 319中添加了功能

    如果您有以前的版本,那么解决方法可以是使用带有LEFT JOIN 的子查询:

    with exploded as (
    select *
        from table t
        cross join unnest (t.id) as t(order_id)
    )
    
    select t.*, e.order_id 
      from table t
           left join exploded e on t.join_key=e.join_key
    

    只需使用正确的连接键

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      相关资源
      最近更新 更多