3

Postgres v12.0

I have a table

data
{"a": "1", "b": "1"}
{"a": "2", "b": "1"}

And I'd like to retrieve a distinct list of keys and the set of values for each key

key values
a [ "1", "2" ]
b [ "1" ]

Not sure how to formulate a query to achieve those results.

1 Answer 1

3

here is one way:

select key , array_agg(distinct value) 
from table
join lateral (select * from jsonb_each_text(datacolumn)) j on true 
group by key 

db<>fiddle here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.