在Presto中添加两张地图

时间:2018-12-05 23:56:21

标签: presto

我的数据如下所述

customer_id   usage_month  usage_by_product         usage
1             June         {"A":50, "B":50}         100
1             July         {"A":50, "B":10, "C":20} 80
1             Aug          {"A":50, "D":500}        550
1             Sep          {"C" :30}                30

我想编写一个查询,汇总全年的总使用量

customer_id   usage_by_product    usage
 1            {"A": 150, "B":60   760
               "C": 50, "D":500}

是否可以在雅典娜(Presto)的地图上进行这种汇总?

1 个答案:

答案 0 :(得分:1)

您可以使用map_entries + UNNEST将地图拆分为单独的键/值对。然后,只需将值求和再聚合回map

例如:

WITH input AS (
    SELECT * FROM (VALUES
        (1, map(array['a', 'c'], array[50, 42])), 
        (1, map(array['a', 'b'], array[50, 18]))
    ) t(customer_id, m)
),
sum_by_map_key AS (
    SELECT customer_id, k, sum(v) AS s
    FROM input
    CROSS JOIN UNNEST(map_entries(m)) AS u(k, v)
    GROUP BY customer_id, k
)
SELECT customer_id, map_agg(k, s)
FROM sum_by_map_key
GROUP BY customer_id;

输出:

 customer_id |        _col1
-------------+---------------------
           1 | {a=100, b=18, c=42}
(1 row)

注意: 要仅添加两个这样的地图,可以使用map_zip_with。但是,要在聚合多行时使用它,可能需要将所有映射值聚合到单个array中,并对其进行数组缩减。根据这些数组的大小,将所有地图聚合到单个array(map)中可能会或可能不会。