Impala从列移到行,列名消失

时间:2019-01-25 10:23:27

标签: sql group-by pivot transpose impala

对于Impala和sql来说,我是新手。我正在尝试执行一些数据透视操作以从此表开始。

输入:

名称表:MyName

+-----------+---------------------+-----------+
| Column A  | Column B            | Column C  |
+-----------+---------------------+-----------+
| a1        | b1                  | c1        |
| a2        | b2                  | c2        |
| a3        | b3                  | c3        |
+-----------+---------------------+-----------+

并获得另一张转置的表,其中b1,b2,b3从列到行。

输出:

+-----------+---------------------+-----------+
| b1        | b2                  | b3        |
+-----------+---------------------+-----------+
| a1        | a2                  | a3        |
| c1        | c2                  | c3        |
+-----------+---------------------+-----------+

这是我到目前为止提出的代码:

select b_column,
       max(case where b_column='b%' then column_a, column_c end) column_a, column_c
  from MyName
  group by b_column;

但是它不起作用,我感觉很卡住。

任何人都可以给我有关如何解决此问题的提示/建议吗?

非常感谢!

2 个答案:

答案 0 :(得分:0)

如果您通常想在imapla中做一个转折,则不能按照6.1文档进行操作,那么PIVOT不是当前功能。

https://www.cloudera.com/documentation/enterprise/6/6.1/topics/impala_reserved_words.html

答案 1 :(得分:0)

select b_column,
       max(case when b_column like 'b%' then column_a end) column_a,
       max(case when b_column like 'c%' then column_c end) column_c
  from MyName
  group by b_column;
相关问题