如何将多行拆分为单行但AWS雅典娜中的多列

How to split multiple rows in to single row but multiple columns in AWS athena

我有如下数据集:

现在,我想将每个索赔获得多条记录的 role_clai 转换为两列或更多列,例如 role_clai1role_clai2,以防我获得 3 条相同的记录ID。我尝试了以下查询:

SELECT client,active,claim,role_polh,role_agnt,
  kv1['CLAI'] AS A_v1,
  kv1['CLAI'] as clai2
FROM (
  SELECT client,active,claim,role_polh,role_agnt,map_agg(claim,role_clai) kv1
  FROM "final_view"
  GROUP BY client,active,claim,role_polh,role_agnt
)  where claim = '00600000000015609'

Output:

Expected output:

role_clai 的两个值应分配给两个新创建的列。

可以看出,对于创建的两列,我没有获得任何值。那我哪里错了?

我认为您应该尝试使用 array_agg 而不是 map_agg,因为整个地图的键都是相似的:

array_agg(x) → array<[same as input]>
Returns an array created from the input x elements.

然后您应该能够使用 role_claims[1]element_at:

访问数组元素
SELECT client,active,claim,role_polh,role_agnt,
element_at(role_claims,1) AS role_clams_1,
element_at(role_claims,2) AS role_claims_2
FROM (
  SELECT client,active,claim,role_polh,role_agnt,array_agg(role_clai) role_claims
  FROM "final_view"
  GROUP BY client,active,claim,role_polh,role_agnt
)  where claim = '00600000000015609'