pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Terry Brick <terry_brick2000@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Formatting Functions and Group By
Date: Tue, 13 Apr 2004 13:33:18 -0400
Message-ID: <24390.1081877598@sss.pgh.pa.us> (raw)
In-Reply-To: <20040413161305.22857.qmail@web11502.mail.yahoo.com>
References: <20040413161305.22857.qmail@web11502.mail.yahoo.com>

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



view thread (4+ messages)  latest in thread

Message-ID: <24390.1081877598@sss.pgh.pa.us>
Permalink:  ../24390.1081877598@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/24390.1081877598@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, terry_brick2000@yahoo.com
  Subject: Re: Formatting Functions and Group By
  In-Reply-To: <24390.1081877598@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox