【问题标题】:Query to fetch data from array_aggregate查询以从 array_aggregate 获取数据
【发布时间】:2022-01-21 22:25:24
【问题描述】:

我在数组中有包含数据的列。

Alphabets    
{A,B,C}
{A}
{B,C}

我需要一个查询,它给我输出为 {A} 的 A

【问题讨论】:

    标签: presto


    【解决方案1】:
    WITH
    dataset AS (
      SELECT ARRAY
      [
        ARRAY[1,2,3,4],
        ARRAY[5,6,7,8],
        ARRAY[9,0]
      ] AS items
    ),
    item AS (
      SELECT i AS array_items
      FROM dataset, UNNEST(items) AS t(i)
    )
    SELECT array_items, sum(val) AS total
    FROM item, UNNEST(array_items) AS t(val)
    GROUP BY array_items;
    

    在最后一个 SELECT 语句中,您可以使用 reduce() 来减少处理时间和数据传输,而不是使用 sum() 和 UNNEST,如下例所示。

    WITH
    dataset AS (
      SELECT ARRAY
      [
        ARRAY[1,2,3,4],
        ARRAY[5,6,7,8],
        ARRAY[9,0]
      ] AS items
    ),
    item AS (
      SELECT i AS array_items
      FROM dataset, UNNEST(items) AS t(i)
    )
    SELECT array_items, reduce(array_items, 0 , (s, x) -> s + x, s -> s) AS total
    FROM item;
    

    任一查询都返回以下结果。返回结果的顺序不保证。

    +----------------------+
    | array_items  | total |
    +----------------------+
    | [1, 2, 3, 4] | 10    |
    | [5, 6, 7, 8] | 26    |
    | [9, 0]       | 9     |
    +----------------------+
    

    参考:

    https://docs.aws.amazon.com/athena/latest/ug/arrays-and-aggregation.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      相关资源
      最近更新 更多