count distinct and group by

From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: count distinct and group by
Date: 2015-05-07 10:23:59
Message-ID: CAFjNrYtrTMFmRVrz=PyWkm-SBQQq28DvTzmsaYuVP-bqjPERDQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,
I'm not sure why there is a reason for such behaviour.

For this table:

create table bg(id serial primary key, t text);

This works:

select count(id) from bg;

This works:

select count(distinct id) from bg;

And this doesn't:

select count(distinct id) from bg order by id;
ERROR: column "bg.id" must appear in the GROUP BY clause or be used in an
aggregate function
LINE 1: select count(distinct id) from bg order by id;

thanks,
Szymon

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Magnus Hagander 2015-05-07 10:39:02 Re: count distinct and group by
Previous Message Tim Clarke 2015-05-07 08:36:44 Re: detached query?