fmsketch_dcount can be run on a column of any type. It returns an approximation to the number of distinct values (a la COUNT(DISTINCT x)), but faster and approximate. Like any aggregate, it can be combined with a GROUP BY clause to do distinct counts per group.
Get the number of distinct values in a designated column.
fmsketch_dcount( col_name )
CREATE TABLE data(class INT, a1 INT); INSERT INTO data SELECT 1,1 FROM generate_series(1,10000); INSERT INTO data SELECT 1,2 FROM generate_series(1,15000); INSERT INTO data SELECT 1,3 FROM generate_series(1,10000); INSERT INTO data SELECT 2,5 FROM generate_series(1,1000); INSERT INTO data SELECT 2,6 FROM generate_series(1,1000);
SELECT class, fmsketch_dcount(a1) FROM data GROUP BY data.class;Result:
class | fmsketch_dcount
------+-----------------
2 | 2
1 | 3
(2 rows)