Re: Trying to group on date parts

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Rob Anderson" <roba(at)bml(dot)uk(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Trying to group on date parts
Date: 2005-04-01 17:07:37
Message-ID: 15164.1112375257@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

"Rob Anderson" <roba(at)bml(dot)uk(dot)com> writes:
> SELECT
> SUBSTRING(mylog.datetime,1,7) AS datetime,
> count(*) as counter
> FROM mylog
> WHERE mylog.datetime<'2005-02-02'
> GROUP BY SUBSTRING(mylog.datetime,1,7)
> ORDER BY mylog.datetime;

> ERROR: column "mylog.datetime" must appear in the GROUP BY clause or be
> used in an aggregate function

Try
ORDER BY SUBSTRING(mylog.datetime,1,7);

Your original isn't legal because there's not a unique value of
mylog.datetime for each group. You know and I know that that doesn't
really matter in this case, but the software is just mechanically
enforcing the SQL rule that says the SELECT and ORDER BY items have
to have unique values in each group.

regards, tom lane

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Ennio-Sr 2005-04-01 18:36:35 Re: how to ignore accents?
Previous Message Keith Worthington 2005-04-01 17:01:10 Calling trigger function from a function