I have a text array field in postgres we can call items
. I want to create a query that will tell me how many times each of these items co-occurs with one another.
For an example set of rows:
items
-----
{'a', 'c'}
{'a', 'b', 'c'}
{'a', 'c'}
{'a', 'b', 'c'}
Here is example output using a :
to separate the item name and the count of co-occurrences:
item|co_occurrences
-------------------
a |{c:4,b:2}
b |{a:2,c:2}
c |{a:4,b:2}
The item
column lists the individual items. The co_occurences
column is an array of text elements combining the co-occurring item and the count. What query will produce this?
2
Answers
Convert it into normal relational form first.
This code assigns arbitrary row
id
values:Working fiddle
If the objective does not have to be JSON, then we can construct a string. This allows ordering the values in the string:
Updated fiddle
Instead of generating row ids for the self-join like @MikeOrganek, I’d just
unnest
twice to produce a relation with duplicate tuples:(online demo)