如何将行转换为AWS Athena中的列?

时间:2017-12-28 19:30:00

标签: sql amazon-web-services pivot amazon-athena

我是AWS Athena的新手并尝试将某些行转换为列,类似于此StackOverflow post中的最佳答案。

然而,当我尝试时:

SELECT column1, column2, column3
FROM data
PIVOT
(
  MIN(column3)
  FOR column2 IN ('VALUE1','VALUE2','VALUE3','VALUE4')
)

我收到错误:mismatched input '(' expecting {',', ')'} (service: amazonathena; status code: 400; error code: invalidrequestexception

有谁知道如何实现我想在AWS Athena中实现的目标?

3 个答案:

答案 0 :(得分:1)

您可以使用PIVOT在Athena中执行单列map_agg

SELECT
  uid,
  kv['c1'] AS c1,
  kv['c2'] AS c2,
  kv['c3'] AS c3
FROM (
  SELECT uid, map_agg(key, value) kv
  FROM vtable
  GROUP BY uid
) t

信用转到this website。不幸的是,我没有找到一种聪明的方法来以这种方式进行多列透视(我嵌套查询,这不是很漂亮)。

答案 1 :(得分:1)

扩展@kadrach的答案。 假设这样的表

uid | key | value1 | value2
----+-----+--------+--------
 1  |  A  |  10    | 1000
 1  |  B  |  20    | 2000
 2  |  A  |  11    | 1001
 2  |  B  |  21    | 2001

单列PIVOT的工作原理如下

SELECT
  uid,
  kv1['A'] AS A_v1,
  kv1['B'] AS B_v1
FROM (
  SELECT uid, map_agg(key, value1) kv1
  FROM vtable
  GROUP BY uid
) 

结果:

uid | A_v1 | B_v1 
----+------+-------
 1  |  10  |  20   
 2  |  11  |  21  

多列PIVOT的工作原理如下

SELECT
  uid,
  kv1['A'] AS A_v1,
  kv1['B'] AS B_v1,
  kv2['A'] AS A_v2,
  kv2['B'] AS B_v2
FROM (
  SELECT uid,
      map_agg(key, value1) kv1,
      map_agg(key, value2) kv2
  FROM vtable
  GROUP BY uid
) 

结果:

uid | A_v1 | B_v1 | A_v2 | B_v2 
----+------+------+------+-----
 1  |  10  |  20  | 1000 | 2000
 2  |  11  |  21  | 1001 | 2001

答案 2 :(得分:0)

使用PIVOT功能时遇到了同样的问题。但是我使用转向方式来获得类似的格式数据集:

select 
  columnToGroupOn,
  min(if(colToPivot=VALUE1,column3,null)) as VALUE1, 
  min(if(colToPivot=VALUE2,column3,null)) as VALUE2, 
  min(if(colToPivot=VALUE3,column3,null)) as VALUE3 
from
    data
group by columnToGroupOn