I have some table with json field. I want to group & count that field to understand its usage frequency. But the data in this field is stored in a random order, so I can get the wrong result. That's why I've decided to sort that field before grouping, but I failed. How can I do it?
JSON_FIELD
["one", "two"]
["two"]
["two", "one"]
["two"]
["three"]
I've tried this code:
SELECT JSON_FIELD, COUNT(1) FROM my_table GROUP BY JSON_FIELD;
But the result w/o sorting is wrong:
JSON_FIELD COUNT
["one", "two"] 1
["two"] 2
["two", "one"] 1
["three"] 1
But if I could sort it somehow, the expected (and correct) result would be:
JSON_FIELD COUNT
["one", "two"] 2
["two"] 2
["three"] 1
My question is very familiar to How to convert json array into postgres int array in postgres 9.3