PostgreSQL Tips: Most common values
When working on the database schema and query optimization it is always useful to have an idea what are the most common met values in the tables. Below is a query retrieveing such a data for a field FLD in the table TABLE.
SELECT
CAST(
UNNEST(string_to_array(
array_to_string(most_common_vals, ','), ','
)) AS integer
) AS most_common_vals
FROM pg_stats
WHERE tablename='TABLE'
AND attname='FLD'