Re: count distinct and group by

From: Geoff Winkless <pgsqladmin(at)geoff(dot)dj>
To: Szymon Guz <mabewlun(at)gmail(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: count distinct and group by
Date: 2015-05-07 10:39:30
Message-ID: CAEzk6ffC_foW_i6-uTvUOxL7PGK3xnir2r4_b0BCMv6UNJpM9g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7 May 2015 at 11:23, Szymon Guz <mabewlun(at)gmail(dot)com> wrote:

> Hi,
> I'm not sure why there is a reason for such behaviour.
>
> 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;
> ​​
>
>
Quite apart from the fact that you're trying to ORDER a recordset that
contains a single row (why?), in Postgres (unlike MySQL) you can't order a
list of values by a column you haven't selected.​

Is this what you're trying to achieve:

SELECT COUNT(*), id FROM bg GROUP BY id ORDER BY id;​

​?​

Geoff

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andomar 2015-05-07 10:46:19 Re: count distinct and group by
Previous Message Magnus Hagander 2015-05-07 10:39:02 Re: count distinct and group by