gpt4 book ai didi

sql - 在 Presto 中压缩数组

转载 作者:行者123 更新时间:2023-12-04 12:17:27 25 4
gpt4 key购买 nike

我有一个使用它们生成数组字符串的查询 array_agg()功能

SELECT 
array_agg(message) as sequence
from mytable
group by id

它产生一个看起来像这样的表:
                 sequence
1 foo foo bar baz bar baz
2 foo bar bar bar baz
3 foo foo foo bar bar baz

但我的目标是压缩字符串数组,以便没有人可以连续重复多次,例如,所需的输出如下所示:
    sequence
1 foo bar baz bar baz
2 foo bar baz
3 foo bar baz

如何使用 Presto SQL 做到这一点?

最佳答案

您可以通过以下两种方式之一执行此操作:

  • 使用 array_distinct 从结果数组中删除重复项功能:

  • WITH mytable(id, message) AS (VALUES
    (1, 'foo'), (1, 'foo'), (1, 'bar'), (1, 'bar'), (1, 'baz'), (1, 'baz'),
    (2, 'foo'), (2, 'bar'), (2, 'bar'), (2, 'bar'), (2, 'baz'),
    (3, 'foo'), (3, 'foo'), (3, 'foo'), (3, 'bar'), (3, 'bar'), (3, 'baz')
    )
    SELECT array_distinct(array_agg(message)) AS sequence
    FROM mytable
    GROUP BY id
  • 使用 DISTINCT聚合中的限定符以在将重复值传递到 array_agg 之前删除它们。

  • WITH mytable(id, message) AS (VALUES
    (1, 'foo'), (1, 'foo'), (1, 'bar'), (1, 'bar'), (1, 'baz'), (1, 'baz'),
    (2, 'foo'), (2, 'bar'), (2, 'bar'), (2, 'bar'), (2, 'baz'), (3, 'foo'),
    (3, 'foo'), (3, 'foo'), (3, 'bar'), (3, 'bar'), (3, 'baz')
    )
    SELECT array_agg(DISTINCT message) AS sequence
    FROM mytable
    GROUP BY id
    两种选择都会产生相同的结果:
        sequence
    -----------------
    [foo, bar, baz]
    [foo, bar, baz]
    [foo, bar, baz]
    (3 rows)
    更新 :您可以使用最近推出的 MATCH_RECOGNIZE 删除重复的元素序列特征:
    WITH mytable(id, message) AS (VALUES
    (1, 'foo'), (1, 'foo'), (1, 'bar'), (1, 'baz'), (1, 'bar'), (1, 'baz'),
    (2, 'foo'), (2, 'bar'), (2, 'bar'), (2, 'bar'), (2, 'baz'),
    (3, 'foo'), (3, 'foo'), (3, 'foo'), (3, 'bar'), (3, 'bar'), (3, 'baz')
    )
    SELECT array_agg(value) AS sequence
    FROM mytable
    MATCH_RECOGNIZE(
    PARTITION BY id
    MEASURES A.message AS value
    PATTERN (A B*)
    DEFINE B AS message = PREV(message)
    )
    GROUP BY id

    关于sql - 在 Presto 中压缩数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56349907/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com