当特定列中有值时,向输出添加重复行

  • 本文关键字:输出 添加 sql amazon-redshift
  • 更新时间 :
  • 英文 :


我需要一个表,它有三列(num_id_1/2/3),具有相同num_id的不同版本,并复制行,以便每个不同的num_id有自己的行,在行中的其余数据重复。我只想在有多个num_id的情况下执行此操作(因此忽略num_id_2或3 cols中的任何空白),因此不会在最终num_id col:

中创建具有空白的最终输出例如:

-----------------------------------------------------
| Id | num_id_1   | num_id_2 | num_id_3 | other rows of data 
----------------------------------------------------
| 1  | 11111      | 2222222  |33333333  | aaaa
| 2  | 12345      |          |          | cccc

:

-----------------------------------------------------
| Id | num_id     | other rows of data 
----------------------------------------------------
| 1  | 11111      | aaaa
| 1  | 2222222    | aaaa
| 1  | 33333333   | aaaa
| 2  | 12345      | cccc

如何构建结果?这是RedShift,我不是很熟悉

在Redshift中,union all可能是最简单的方法:

select id, num_id_1 as num_id, other columns
from t
where num_id_1 is not null
union all
select id, num_id_2 as num_id, other columns
from t
where num_id_2 is not null
union all
select id, num_id_3 as num_id, other columns
from t
where num_id_3 is not null;

最新更新