【问题标题】:How to merge rows in redshift如何在redshift中合并行
【发布时间】:2019-04-29 15:02:22
【问题描述】:

我有一个 redshift 表,其中包含一个带有通用标识符的列,以及许多其他属性列。但是,通用标识符出现在多行中,每个属性对应一行。我想合并它们。

Common ID | Attribute 1 | Attribute 2
123          X             null
123          null          Y
987          null          A
987          B             null

想把它变成

Common ID | Attribute 1 | Attribute 2
123         X             Y
987         B             A

这基本上开始于我旋转一个表(使用 case 语句),但最终输出需要按公共 id 合并或分组(但没有聚合)。

请注意,最终产品中大约有 20 个属性,因此与属性数量无关的解决方案是理想的。这也意味着 common id 最多可以出现 20 次左右。

我查看了 listagg 但希望最终输出具有相同数量的列,而不是一个聚合/连接列。

【问题讨论】:

    标签: sql amazon-redshift


    【解决方案1】:

    使用聚合:

    select common_id, max(attribute_1) as attribute_1, max(attribute_2) as attribute_2
    from t
    group by common_id;
    

    您可以通过修复group by 键在生成数据的查询中解决此问题。

    【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 2016-06-10
    • 2021-03-13
    • 1970-01-01
    • 2022-08-03
    相关资源
    最近更新 更多