Re: Formatting Functions and Group By - Mailing list pgsql-sql

From Tom Lane
Subject Re: Formatting Functions and Group By
Date
Msg-id 24390.1081877598@sss.pgh.pa.us
Whole thread Raw
In response to Formatting Functions and Group By  (Terry Brick <terry_brick2000@yahoo.com>)
Responses Re: Formatting Functions and Group By  (Terry Brick <terry_brick2000@yahoo.com>)
List pgsql-sql
Terry Brick <terry_brick2000@yahoo.com> writes:
> 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')

Ah, good ol' MySQL :-( ... let the user do what he wants whether the
result is well defined or not ...

I'd suggest doing the grouping/ordering numerically rather than
textually.  For instance,

select to_char(date_trunc('month', myCol), 'Mon YY')
from myTable
group by date_trunc('month', myCol)
order by date_trunc('month', myCol);

Now this assumes you really want a time-based ordering, which the quoted
example doesn't give --- you've got month sorting to the left of year,
is that really what you want?  If it is then you'd need to go

group by date_trunc('month', myCol)
order by to_char(date_trunc('month', myCol), 'MM YYYY')
        regards, tom lane


pgsql-sql by date:

Previous
From: Bruno Wolff III
Date:
Subject: Re: Formatting Functions and Group By
Next
From: Greg Stark
Date:
Subject: Re: trigger/for key help