count function alternative in postgres

Started by junaidmalik14about 16 years ago5 messagesgeneral
Jump to latest
#1junaidmalik14
junaidmalik14@gmail.com

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.

Reference url is given below

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count-distinct

Thanks

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: junaidmalik14 (#1)
Re: count function alternative in postgres

junaid malik <junaidmalik14@gmail.com> writes:

Is there any alternative of mysql function COUNT(DISTINCT expr,
[expr...]) in postgres. We get error if we

The SQL-standard way to do that would be

select count(*) from (select distinct expr,expr,... from ...) as ss;

COUNT with multiple arguments is not anywhere in the standard.

regards, tom lane

#3Scott Bailey
artacus@comcast.net
In reply to: junaidmalik14 (#1)
Re: count function alternative in postgres

junaid malik wrote:

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.

Reference url is given below

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count-distinct

Thanks

I already answered your question on dbforums.com. But in Postgres you
can do:

SELECT COUNT( DISTINCT row(col1, col2, col3) ) FROM foo
or
SELECT COUNT( DISTINCT (col1, col2, col3) ) FROM foo

#4Merlin Moncure
mmoncure@gmail.com
In reply to: Scott Bailey (#3)
Re: count function alternative in postgres

On Sat, Apr 3, 2010 at 3:02 PM, Scott Bailey <artacus@comcast.net> wrote:

junaid malik wrote:

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.

Reference url is given below

http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count-distinct

Thanks

I already answered your question on dbforums.com. But in Postgres you can
do:

SELECT COUNT( DISTINCT row(col1, col2, col3) ) FROM foo
or
SELECT COUNT( DISTINCT (col1, col2, col3) ) FROM foo

very clever! This is similar to how I use rowtypes to get around the
single column restrictions on function calls in the select field list.

merlin

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Merlin Moncure (#4)
Re: count function alternative in postgres

Merlin Moncure <mmoncure@gmail.com> writes:

On Sat, Apr 3, 2010 at 3:02 PM, Scott Bailey <artacus@comcast.net> wrote:

SELECT COUNT( DISTINCT row(col1, col2, col3) ) FROM foo

very clever! This is similar to how I use rowtypes to get around the
single column restrictions on function calls in the select field list.

Cute, but note it will only work in 8.4 or later ...

regards, tom lane