我有下表,
_________________
|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"时,我遇到了错误磁盘利用率已满......我有数十亿条记录。
答案 0 :(得分:2)
由于问题标记为Oracle,您可以执行以下操作:
SELECT id, key
FROM table_name
UNPIVOT INCLUDE NULLS ( key FOR key_name IN ( key1, key2, key3 ) );
答案 1 :(得分:0)
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
表引用可以是任何至少包含3行的表。
答案 2 :(得分:-1)
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