在红移中将一行拆分为多条记录

split one row to multiple record in redshift

我关注table,

     _________________
    |id key1 key2 key3|
    ------------------
     1  101  102  103
     2  201  202  203

我需要一个将创建以下输出的查询,

    |id key|
    --------
     1  101  
     1  102
     1  103
     2  201
     2  202
     2  203

除了union all还有别的吗?当我使用 "union all" 时,我遇到了磁盘利用率已满的错误...我有数十亿条记录。

select a.id, case b.l when 1 then a.key1 when 2 then a.key2 else a.key3 end key
from mytable a
cross join (select level l from dual connect by level < 4) b
order by 1,2

由于问题被标记为 Oracle,您可以这样做:

SELECT id, key
FROM   table_name
UNPIVOT INCLUDE NULLS ( key FOR key_name IN ( key1, key2, key3 ) );

union all 在 Redshift 上非常有效。怀疑有什么更好的。

create table new_table as
select id, key1 as key from old_table
union all
select id, key2 as key from old_table
union all
select id, key3 as key from old_table

如果您想尝试像 Mottor 这样的东西,建议您可以用 Redshift hack 替换 Oracle 特定的 dual connect by level 位,如下所示:

(select row_number() over (order by true) as l from stv_blocklist limit 3) b

stv_blocklist table 引用可能有任何 table 至少有 3 行。