问题
(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