Is there an efficient way to aggregate data from a jsonb
column in postgresql? Given the table
userdata(id STRING, data JSONB)
and the data
123.abc, { "dis": ["close"] }
123.abc, { "purpose": {"score": 0.1, "text": "hi"}, "dis": ["hello", "close"]}
123.abc, { "dis": ["bye"], "dir": 1}
123.abc, {}
567.bhy, { "dis", ["close"]}
I’m trying to get the following result:
123.abc, { "dis": ["close", "hello", "bye"], "purpose": {"score": 0.1, "text": "hi", "dir": 1}
567.bhy, {"dis", ["close"]}
As you notice, I want unique values of keys (for e.g. "dis"
) across all records indexed by the id.
I tried getting values using jsonb_agg
and jsonb_array_elements
. I could not aggregate all keys and distinct values. I couldn’t figure out how to use jsonb_each
to get all keys
What I tried is something like this to get one key.
select id,
(select jsonb_agg(t->>'dis') from jsonb_array_elements(data::jsonb) as x(t) where t->>'dis' is not null) as sdata
from mytable where id='123.abc'
Any help with the query is appreciated.