小编Kat*_*res的帖子

数组联合作为聚合函数

我有以下输入:

name  | count | options
-----------------------
user1 | 3     | ['option1', 'option2']
user1 | 12    | ['option2', 'option3']
user2 | 2     | ['option1', 'option3']
user2 | 1     | []
Run Code Online (Sandbox Code Playgroud)

我想要以下输出:

name  | count | options
-----------------------
user1 | 12    | ['option1', 'option2', 'option3']
user2 | 2     | ['option1', 'option3']
Run Code Online (Sandbox Code Playgroud)

我按名字分组.对于每个组,计数应该聚合为,max并且选项应该聚合为union.我正在弄清楚后者是怎么做的.

目前,我有这个查询:

with data(name, count, options) as (
    select 'user1', 12, array['option1', 'option2']::text[]
    union all
    select 'user1', 12, array['option2', 'option3']::text[]
    union all
    select 'user2', 2, array['option1', …
Run Code Online (Sandbox Code Playgroud)

sql arrays postgresql aggregate-functions

8
推荐指数
1
解决办法
784
查看次数

标签 统计

aggregate-functions ×1

arrays ×1

postgresql ×1

sql ×1