Skip to content
Advertisement

How to group results by values that are inside json array in postgreSQL

I have a column of type jSONB that have data like this:

column name: used_filters

row number 1 example:

row number 2 example:

I want to group the result to get the most frequent value for each one of the keys something like this:

key most_freq
category economic
tags eco-friendly
keyword 2050

the keys are not constant and could be something other than the example I said but I know that they will be frequent.

Advertisement

Answer

You can extract keys and values as arrays first by using jsonb_each, and then unnest the generated arrays by jsonb_array_elements_text. The rest is classical aggregation along with sorting through the count values by window function such as

Demo

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement