简体   繁体   English

SQL presto - 交叉连接未嵌套 null 值

[英]SQL presto - cross join unnest null value

I have arrays of different sizes and I want each value in the array to be in separate rows.我有不同大小的 arrays ,我希望数组中的每个值都位于不同的行中。 To do that, I have used the cross join unnest.为此,我使用了交叉连接 unnest。 It is working however, it is deleting null array.但是它正在工作,它正在删除 null 数组。

So, I have my column ID with the different arrays and some are nulls, when I do因此,当我这样做时,我的列 ID 具有不同的 arrays 并且有些是空值

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

I only get the following results我只得到以下结果

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

and I want而且我要

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

If someone knows how to unnest null values it would be much appreciated.如果有人知道如何取消嵌套 null 值,将不胜感激。 I've been looking on the internet and I saw that we could include a WITH ORDINALITY clause but I don't know how it works.我一直在网上寻找,我看到我们可以包含一个 WITH ORDINALITY 子句,但我不知道它是如何工作的。

Use LEFT JOIN UNNEST instead of CROSS JOIN UNNEST .使用LEFT JOIN UNNEST而不是CROSS JOIN UNNEST Feature was added in Presto 319 Presto 319中添加了功能

If you have previous version, then the workaround can be using subquery with LEFT JOIN :如果您有以前的版本,那么解决方法可以使用带有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

Just use correct join key只需使用正确的加入密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM