基于另一个字段将多个列合并为一个

Combining Multiple Columns into One based off another field

我正在使用 AWS Athena 并希望将多个列合并到一个新字段中。

示例:上面的 table 显示了我的数据集的样子。

我想要的输出是:

所以只有当其中有一个值时,它才会合并到一列中,并且对于 ID。

使用这个:

Select MyId,Col1 as CombininedCol from YourTable where Col1 is not null
Union All
Select MyId,Col2 as CombininedCol from YourTable where Col2 is not null
Union All
Select MyId,Col3 as CombininedCol from YourTable where Col3 is not null
Union All
Select MyId,Col4 as CombininedCol from YourTable where Col4 is not null
order by MyId
 

提示:UNION ALL 命令合并了两个或多个 SELECT 语句的结果集(允许重复值)。

每个select分别得到MyID和Cole 1,2等两列,最后returns作为两列

您可以将列合并到数组中 (array[col1, col2, ...]),filter out nulls and then unnest:

-- sample data
WITH dataset (MyID, col1, col2) AS (
    VALUES  
        (1, null, 'RED'),
        (2, 'GREEN', 'RED')
) 

--query
select myid,
    c as CombinedColumn
from dataset
    cross join unnest (filter(array[col1, col2], s->s is not null)) as t(c) t(c)

输出:

myid CombinedColumn
1 RED
2 GREEN
2 RED