Re: count function alternative in postgres

From: Ben Morrow <ben(at)morrow(dot)me(dot)uk>
To: junaidmalik14(at)gmail(dot)com, pgsql-sql(at)postgresql(dot)org
Subject: Re: count function alternative in postgres
Date: 2010-04-06 18:47:14
Message-ID: 20100406184714.GA3290@osiris.mauzo.dyndns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Quoth junaidmalik14(at)gmail(dot)com (junaidmalik14):
>
> Is there any alternative of mysql function COUNT(DISTINCT expr,[expr...]) in
> postgres. We get error if we
>
> write count like this count(distinct profile.id, profile.name, profile.age)
> but it works well in mysql.

Pg does support COUNT(DISTINCT ), but only for a single column. The best
I can come up with for multiple columns is

select count(distinct profile.tuple) from
(select (id, name, age) as tuple from profile)
as profile;

or alternatively

select count(*) from
(select distinct (id, name, age) as tuple from profile)
as profile;

Ben

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Glaesemann 2010-04-06 22:04:54 Re: Table Design for Hierarchical Data
Previous Message Thomas Kellerer 2010-04-06 17:39:31 Re: count function alternative in postgres