Postgresql - Count freuency of array or jsonb object

有些话、适合烂在心里 提交于 2021-02-11 12:15:20

问题


In pg, there is a tags field of type varchar, containing tags separated by ], e.g 'a]b]c'.

Need to count occurrences of these tags across multiple rows.

Currently, I know how to:

  • Convert the raw string into pg array ['a', 'b', 'c']
  • and if the column is given as jsonb object {'a':1, 'b':1, 'c':1}, could count frequency via jsonb functions.

But I don't know how to convert pg array ['a', 'b', 'c'] into jsonb object {'a':1, 'b':1, 'c':1}, or just count frequency on array elements directly.

The questions are:

  • A. How to convert text 'a]b]c' into jsonb object {'a':1, 'b':1, 'c':1}, with all values as 1.
  • B. How to count frequency of array elements ['a', 'b', 'c'], across multiple rows.

If either of the 2 questions can be solved, the original problem could be solved.
Or, there are even a better solution?


@Update - To make the question more clear

If the input column is already json object, not raw string or array.

Following table show what I want to do:

-- create table,
create table json_aggr_learn (
    id serial8 primary key,
    uid int8,
    freq jsonb,
    created_at timestamptz default current_timestamp
);

-- init data
insert into json_aggr_learn(uid, freq) values
(1, '{"a":1, "b":2}'),
(1,'{"b":2, "c":4}'),
(2, '{"a":1, "b":2}'),
(2,'{"b":7, "c":4}'),
(2,'{"e":10, "c":4}'),
(3,'{"a":5, "c":4, "f":2}');

select * from json_aggr_learn limit 5;

-- aggr
select uid, jsonb_object_agg(key, value) as merged_freq
from
     (select id, uid, key, value
      from json_aggr_learn, jsonb_each_text(freq)
     ) as expended
group by uid
order by uid;

Here is the output of aggr sql:


回答1:


You can unnest() arrays, e.g.:

select id, jsonb_object_agg(tag, count) as tags
from (
    select id, unnest(string_to_array(tags, ']')) as tag, count(*)
    from my_table
    group by 1, 2
    ) s
group by 1
order by 1

Db<>fiddle.



来源:https://stackoverflow.com/questions/65785117/postgresql-count-freuency-of-array-or-jsonb-object

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