(这是Postgresql - Count freuency of array or jsonb object之后的后续问题)
在postgresql12+ 中,给出以下输入行:
输出expected是:
uid tag_freq
1 {'a':2, 'b':1, 'c':1}
2 {'a':1, 'b':2, 'c':2, 'e':1}
...
Run Code Online (Sandbox Code Playgroud)
输出列tag_freq是jsonb对象,它是用户的合并结果。
有没有办法写这样的查询?
小智 5
您可以为此使用jsonb_object_agg() :
select uid, jsonb_object_agg(tag, count) as tag_freq
from the_table
group by uid;
Run Code Online (Sandbox Code Playgroud)