【问题标题】:Transpose non Numeric column in Redshift在 Redshift 中转置非数字列
【发布时间】:2019-07-16 16:16:22
【问题描述】:

我有一个如下所示的数据集: 索引、键、值

1, Description, Apple
2, Type, Orange
3, Desciption, Apple
4, Type, Pickle
5, Type, Orange 

我知道如果 Value 是数字的话可以这样做:

select Index,
max(Case when key = 'Description' then Value else null end)
max(case when key = 'type' the value else null end)
from table
group by Index

但鉴于我的值列是一个字符串,这不适用于我的用例。请记住这个 Redshift,所以它没有所有的 postgres 功能。 样本期望输出:

Index, Description,type
1, Apple, Orange
2, Apple, Pickle

【问题讨论】:

    标签: sql amazon-web-services amazon-redshift transpose


    【解决方案1】:

    您想要的输出没有意义,因为每个 Index 值只有一个输入行。因此,无法将行分组为组合值。

    如果您的输入数据是:

    1, Description, Apple
    1, Type, Delicious
    2, Description, Banana
    2, Type, Big
    3, Description, Pineapple
    3, Type, Prickly
    

    然后你可以使用这样的查询:

    SELECT
      index,
      MAX(CASE WHEN description = 'Description' THEN type END) as description,
      MAX(CASE WHEN description = 'Type' THEN type END) as type
    FROM food
    GROUP BY index
    

    输出将是:

    1   Apple      Delicious
    2   Banana     Big
    3   Pineapple  Prickly
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多