首页 > 解决方案 > 在 Redshift 中用一个结果列选择不同的多列

问题描述

我想在 Redshift 下面执行。它可以在 Postgrsql 中使用 unnest 处理,但不能在 Redshift 中工作

其实我有像

id  col_a  col_b  col_c
1   ABD    CDE    XYZ
2   CDE    null   null
3   ABD    null   null
3   FGH    LMN   null

我期望结果

ABC
ABD
CDE
FGH
LMN
XYZ

标签: amazon-redshift

解决方案


对于UNION每一列,获取不同的值:

select col_a as col from tablename
where col_a is not null
union
select col_b from tablename
where col_b is not null
union
select col_c from tablename
where col_c is not null
order by col 

推荐阅读