【问题标题】:Group by array values in Presto在 Presto 中按数组值分组
【发布时间】:2019-12-18 00:20:56
【问题描述】:

有一张像这样的电影表:

genre               revenue
array<string>           int
---------------------------
[Drama]               10000
[Drama, Fiction]       2000
[Fiction]               300
[Comedy]               5000
[Comedy, Fiction]       500

如何按包含的类型对电影进行分组并执行聚合,例如:

genre       count_movies    sum_revenue
---------------------------------------
Drama                  2          12000
Fiction                3           2800
Comedy                 2           5500

我查看了array functions and operators 的文档,但在那个方向上找不到任何东西。

负责此输出的查询可能如下所示:

SELECT
  COUNT(*) AS count_movies,
  SUM(revenue) AS sum_revenue
FROM movies
GROUP BY ARRAY_EXPLODE(genre) -- this is fake, just to illustrate the point

【问题讨论】:

    标签: sql group-by presto


    【解决方案1】:

    你可以使用UNNEST:

    -- pseudocode
    SELECT t.genre,
      COUNT(*) AS count_movies,
      SUM(revenue) AS sum_revenue
    FROM movies
    CROSS JOIN UNNEST(genre) AS t(genre)
    GROUP BY t.genre
    

    【讨论】:

      猜你喜欢
      • 2014-02-19
      • 2019-11-08
      • 2021-12-05
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      相关资源
      最近更新 更多