You can use one of hstore
functions to convert hstore
to another data type that can retrieve the number of elements. For example, you can use avalue
to count the number of keys in an hstore
value:
SELECT id, array_length(avals(my_hstore_field), 1) AS count_keys FROM mytable;
There is a good example in the docs to get all the keys and the number of occurrences that you might need:
SELECT key, count(*) FROM (SELECT (each(h)).key FROM testhstore) AS stat GROUP BY key ORDER BY count DESC, key;
source share