【问题标题】:how to convert a column to an array in presto如何在presto中将列转换为数组
【发布时间】:2021-10-20 23:41:00
【问题描述】:

Postgres 提供了一种将列转换为数组的方法。示例查询:

WITH
    dataset AS (
        SELECT '1' as a
        union
        SELECT '2' as a
        union
        SELECT '3' as a
    )
SELECT
    array(select a from dataset) as array_result;

返回

| array_result |
|--------------|
| {2,3,1}      |

如何使用 Presto SQL 做同样的事情?

【问题讨论】:

    标签: sql arrays aggregate-functions presto


    【解决方案1】:

    使用array_agg(x [ORDER BY y])聚合函数:

    WITH
        dataset AS (
            SELECT '1' as a
            union all
            SELECT '2' as a
            union all
            SELECT '3' as a
        )
    SELECT
        array_agg(a) as array_result from dataset;
    

    结果:

    ['3', '2', '1']
    

    如果需要array<int>,请在聚合前强制转换:array_agg(cast(a as int))

    【讨论】:

      猜你喜欢
      • 2019-10-03
      • 2020-12-24
      • 2021-09-10
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2021-05-06
      • 2019-08-12
      • 2020-08-14
      相关资源
      最近更新 更多