本文介绍了PostgreSQL-将行合并到jsonb对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(这是 Postgresql-计算数组或jsonb对象的频率之后的跟进问题)
(This is a follow up question after Postgresql - Count freuency of array or jsonb object)
在postgresql
12+中,给出以下输入行:
In postgresql
12+, given following input rows:
expected
输出为:
uid tag_freq
1 {'a':2, 'b':1, 'c':1}
2 {'a':1, 'b':2, 'c':2, 'e':1}
...
输出列tag_freq
是jsonb对象,它是用户的合并结果.
Output column tag_freq
is jsonb object, and it's merged result for a user.
有什么办法可以编写这样的查询?
Is there any way to write such a query?
推荐答案
您可以使用 jsonb_object_agg()为此:
select uid, jsonb_object_agg(tag, count) as tag_freq
from the_table
group by uid;
这篇关于PostgreSQL-将行合并到jsonb对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!