Re: Formatting Functions and Group By

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Terry Brick <terry_brick2000(at)yahoo(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Formatting Functions and Group By
Date: 2004-04-13 16:45:18
Message-ID: 20040413164518.GA18956@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, Apr 13, 2004 at 09:13:05 -0700,
Terry Brick <terry_brick2000(at)yahoo(dot)com> wrote:
> Hi,
> I'm porting a bunch of queries from MySQL to Postgres 7.4 and am having a problem with one
> particular area. For example, a query like this works in MySQL:
>
> select
> to_char(myCol,'Mon YY')
> from
> myTable
> group by
> to_char(myCol,'MM YYYY')
> order by
> to_char(myCol,'MM YYYY')
>
> Postgres will give me an error saying that "to_char(myCol,'Mon YY')" must be in the 'group by'. I
> understand why that normally needs to be the case, but in theory, it would be ok here because they
> are actually the same values (in the select and group by) but just formatted differently. I just
> want the query to be grouped and ordered by month and year, but to be formatted differently in the
> output.
>
> Any ideas?

You could do something like the following. It works in 7.4, but I am not
sure if it is completely standard.

area=> select to_char(date_trunc('month',current_date), 'MM YYYY')
area-> group by date_trunc('month',current_date);
to_char
---------
04 2004
(1 row)

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2004-04-13 17:33:18 Re: Formatting Functions and Group By
Previous Message Terry Brick 2004-04-13 16:13:05 Formatting Functions and Group By