【问题标题】:AWS Athena (Presto) how to transpose map where the values are an array to rowsAWS Athena(Presto)如何将值是数组的映射转置为行
【发布时间】:2021-09-17 18:27:41
【问题描述】:

我正在尝试将 Athena 上的复杂结构展平

我的表格中的一行看起来像

id tags
2003301084 {repeats=[3], copayment=[CoPayment, other], concessionalstatus=[E, T], prescriberdate=[2014-06-29], previouspurchasesonsamescript=[0], age=[49], numberofscripts=[1]}

我想去:

id tag value
2003301084 repeats 3
2003301084 copayment CoPayment
2003301084 copayment other
2003301084 concessionalstatus E
2003301084 concessionalstatus T
2003301084 prescriberdate 2014-06-29
2003301084 previouspurchasesonsamescript 0
2003301084 age 49
2003301084 numberofscripts 1

我找不到这样深入结构的示例

【问题讨论】:

    标签: amazon-athena presto


    【解决方案1】:

    您可以使用UNNEST 将映射键/值对提取到单独的行中,然后将各个数组元素提取到单独的行中:

    WITH data(id, tags) AS
    (
        VALUES (
            2003301084,
            map_from_entries(array[
                row('repeats', array['3']),
                row('copayment', array['CoPayment', 'other']),
                row('concessionalstatus', array['E', 'T']),
                row('prescriberdate', array['2014-06-29']),
                row('previouspurchasesonsamescript', array['0']),
                row('age', array['49']),
                row('numberofscripts', array['1'])
            ])
        )
    ),
    arrays AS (
        SELECT id, tag, entries
        FROM data CROSS JOIN UNNEST(tags) AS m(tag, entries)
    )
    SELECT id, tag, value
    FROM arrays CROSS JOIN UNNEST(entries) AS a(value)
    

    =>

         id     |              tag              |   value
    ------------+-------------------------------+------------
     2003301084 | repeats                       | 3
     2003301084 | copayment                     | CoPayment
     2003301084 | copayment                     | other
     2003301084 | concessionalstatus            | E
     2003301084 | concessionalstatus            | T
     2003301084 | prescriberdate                | 2014-06-29
     2003301084 | previouspurchasesonsamescript | 0
     2003301084 | age                           | 49
     2003301084 | numberofscripts               | 1
    (9 rows)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      • 2021-09-10
      • 2018-10-11
      相关资源
      最近更新 更多