X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 2DABAD1CCB4 for ; Tue, 13 Apr 2004 14:33:25 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 11429-03 for ; Tue, 13 Apr 2004 14:33:19 -0300 (ADT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 40B5AD1BAFC for ; Tue, 13 Apr 2004 14:33:18 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i3DHXIfh024391; Tue, 13 Apr 2004 13:33:18 -0400 (EDT) To: Terry Brick Cc: pgsql-sql@postgresql.org Subject: Re: Formatting Functions and Group By In-reply-to: <20040413161305.22857.qmail@web11502.mail.yahoo.com> References: <20040413161305.22857.qmail@web11502.mail.yahoo.com> Comments: In-reply-to Terry Brick message dated "Tue, 13 Apr 2004 09:13:05 -0700" Date: Tue, 13 Apr 2004 13:33:18 -0400 Message-ID: <24390.1081877598@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/119 X-Sequence-Number: 17494 Terry Brick 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