Kat*_*res 8 sql arrays postgresql aggregate-functions
我有以下输入:
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', 'option3']::text[]
union all
select 'user2', 1, array[]::text[]
)
select name, max(count)
from data
group by name
Run Code Online (Sandbox Code Playgroud)
我知道这可以通过定义自定义聚合函数轻松完成,但我想通过查询来完成.我理解unnest()数组的基础知识(以及array_agg()后面的结果),但无法弄清楚如何在我的查询中注入它.
unnest(options)您可以在列表中使用隐式横向连接FROM,然后使用array_agg(distinct v)以下选项创建数组:
with data(name, count, options) as (\n select 'user1', 12, array['option1', 'option2']::text[]\n union all\n select 'user1', 12, array['option2', 'option3']::text[]\n union all\n select 'user2', 2, array['option1', 'option3']::text[]\n union all\n select 'user2', 1, array[]::text[]\n)\nselect name, array_agg(distinct v) -- the 'v' here refers to the 'f(v)' alias below\nfrom data, unnest(options) f(v)\ngroup by name;\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 name \xe2\x94\x82 array_agg \xe2\x94\x82\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n\xe2\x94\x82 user1 \xe2\x94\x82 {option1,option2,option3} \xe2\x94\x82\n\xe2\x94\x82 user2 \xe2\x94\x82 {option1,option3} \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n(2 rows)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
784 次 |
| 最近记录: |