Re: count distinct and group by

From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: count distinct and group by
Date: 2015-05-07 10:48:09
Message-ID: CAFjNrYvbHS8XSp-vQApRKDTWmOq8OxCFaejk_WyF75-6DDTMHQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7 May 2015 at 12:39, Magnus Hagander <magnus(at)hagander(dot)net> wrote:

> On Thu, May 7, 2015 at 12:23 PM, Szymon Guz <mabewlun(at)gmail(dot)com> wrote:
>
>> 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;
>>
>>
> There is no "id" column in the returned dataset to order by. You are just
> returning one value, how would it be ordered? (and that row has a column
> named "count" - but you can alias it to SELECT count(distinct id) AS id
> FROM bg ORDER BY id - it just makes no sense to order a single row..
>
>
Oh, right. Thanks. I haven't noticed that there is no id column in the
dataset.

thanks,
Szymon

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Kellerer 2015-05-07 10:54:46 Re: count distinct and group by
Previous Message Andomar 2015-05-07 10:46:19 Re: count distinct and group by