count & distinct

Started by Andrea Aimealmost 26 years ago3 messagesgeneral
Jump to latest
#1Andrea Aime
aaime@comune.modena.it

Hi people. I would like to make a query that
tells me how many distinct values there are
in a column. Standard count doesn't seems
to support a count distinct option.
select distint count(*) of course doens't
work (distinti clause is applied after the
result are calculated). I've tried to define
a view, but it didn't worked (
create view distValues as select distinct ...
but views doesn't support distinct clause)...

Well, maybe I should create a new aggregate
function, but before spending time on PGSQL
guide I would like to know if someone can
give me a fast tip... ;-)
Thanks in advance
Andrea

PS: well, of course I can open a cursor on
a "select distinct column from table" and then
loop over the cursor couting how many values
there are, but it seem a bit ugly...

#2Andrew Snow
als@fl.net.au
In reply to: Andrea Aime (#1)
RE: count & distinct

Hi people. I would like to make a query that
tells me how many distinct values there are
in a column. Standard count doesn't seems
to support a count distinct option.
select distint count(*) of course doens't
work (distinti clause is applied after the
result are calculated). I've tried to define
a view, but it didn't worked (
create view distValues as select distinct ...
but views doesn't support distinct clause)...

SELECT COUNT(DISTINCT field_name) FROM table_name;

Works for me in v7.

Regards,
Andrew Snow
als@fl.net.au

#3Vashenko Maxim
max@nino.ru
In reply to: Andrea Aime (#1)
Re: count & distinct

Andrea Aime wrote:

Hi people. I would like to make a query that
tells me how many distinct values there are
in a column. Standard count doesn't seems
to support a count distinct option.
select distint count(*) of course doens't
work (distinti clause is applied after the
result are calculated). I've tried to define
a view, but it didn't worked (
create view distValues as select distinct ...
but views doesn't support distinct clause)...

Well, maybe I should create a new aggregate
function, but before spending time on PGSQL
guide I would like to know if someone can
give me a fast tip... ;-)
Thanks in advance
Andrea

PS: well, of course I can open a cursor on
a "select distinct column from table" and then
loop over the cursor couting how many values
there are, but it seem a bit ugly...

SELECT field, count(*) FROM table GROUP BY field

With best regards, Max Vaschenko,
Nizhny Novgorod Information Networks.