我有如下所示的数据集:

现在,我想将每个索赔获得多个记录的role_clai转换为两个或多个列,比如role_clai1和role_clai2,以防我得到相同ID的3条记录。
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的两个值应该在两个新创建的列之间进行划分。
可以看到,对于创建的两列,我没有得到任何值。那我哪里出问题了?
发布于 2020-10-30 09:26:01
我认为您应该尝试使用array_agg而不是map_agg,因为整个地图的关键是相似的:
array_agg(x)→array
返回从输入x元素创建的数组。
然后,您应该能够使用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'https://stackoverflow.com/questions/64604663
复制相似问题