I have a Postgresql table with a json column named "food".
Here is an example of some rows:
food
["cheese", "salmon", "eggs"]
["salmon", "cheese", "eggs"]
["broccoli", "ham", "milk"]
["salmon", "cheese", "eggs", "pizza"]
Current result:
food count
["cheese", "salmon", "eggs"] | 1
["salmon", "cheese", "eggs"] | 1
["broccoli", "ham", "milk"] | 1
["salmon", "cheese", "eggs", "pizza"] | 1
Desired result:
food count
["cheese", "salmon", "eggs"] | 2
["broccoli", "ham", "milk"] | 1
["salmon", "cheese", "eggs", "pizza"] | 1
Is there a way to GROUP BY the contents of a json field without regard to the order of the elements? If two rows have the same contents, then I want them to be grouped together.
My plan was to GROUP BY json_array_elements(food), but for some reason this only returns the first element of each row.