Postgresql - Merge rows into jsonb object

别说谁变了你拦得住时间么 提交于 2021-01-29 15:34:43

问题


(This is a follow up question after Postgresql - Count freuency of array or jsonb object)

In postgresql 12+, given following input rows:

The expected output is:

uid   tag_freq

1     {'a':2, 'b':1, 'c':1}
2     {'a':1, 'b':2, 'c':2, 'e':1}
...

Output column tag_freq is jsonb object, and it's merged result for a user.

Is there any way to write such a query?


回答1:


You can use jsonb_object_agg() for this:

select uid, jsonb_object_agg(tag, count) as tag_freq
from the_table
group by uid;


来源:https://stackoverflow.com/questions/65787512/postgresql-merge-rows-into-jsonb-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!