I have a column stored as jsonb which is an array of number. Values look like this
I’d like to count the total distinct values from this column. I can get the distinct values like below
SELECT DISTINCT JSONB_ARRAY_ELEMENTS_TEXT(aimjoined) AS aims FROM mytable;
I can’t figure out how to count the total number of occurences for each of the unique values.
2
Answers
I ended up also finding the solution below. It seems to perform slightly faster than that by @Stefanov.sm.
First flatten the table using a lateral join and then do a trivial
group by
.