X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 48B4B9DC896 for ; Tue, 24 Jan 2006 22:39:11 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 10796-04 for ; Tue, 24 Jan 2006 22:39:15 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id D73BE9DC85B for ; Tue, 24 Jan 2006 22:39:08 -0400 (AST) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id 0E41E30953; Wed, 25 Jan 2006 03:39:13 +0100 (MET) From: Joseph Shraibman X-Newsgroups: pgsql.sql Subject: Re: sorting by day of the week Date: Tue, 24 Jan 2006 21:39:11 -0500 Organization: Hub.Org Networking Services Lines: 39 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@news.hub.org User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20050920 X-Accept-Language: en-us, en In-Reply-To: To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] X-Spam-Score: 0.12 X-Spam-Level: X-Archive-Number: 200601/156 X-Sequence-Number: 23945 Nevermind, I figured out that I just needed to do it like this: SELECT to_char( logtime, 'Dy'),count(*),to_char( logtime, 'D') FROM sclog WHERE date_trunc('day', logtime) > current_date + '7 day ago'::interval group by to_char( logtime, 'Dy'),to_char( logtime, 'D') ORDER BY to_char( logtime, 'D') DESC; It is interesting that I can't put to_char( logtime, 'D') in the the group by without putting it in the select. Joseph Shraibman wrote: > p8:owl=>SELECT to_char( logtime, 'Dy'),count(*) FROM logtab WHERE > date_trunc('day', logtime) > current_date + '7 day ago'::interval group > by to_char( logtime, 'Dy') ORDER BY to_char( logtime, 'Dy') DESC; > to_char | count > ---------+------- > Wed | 1447 > Tue | 618 > Thu | 1161 > Sun | 230 > Sat | 362 > Mon | 760 > Fri | 1281 > (7 rows) > > The problem is that I want those results sorted in day of week order, > not text order of the day name, so I tried this: > > p8:owl=>SELECT to_char( logtime, 'Dy'),count(*) FROM sclog WHERE > date_trunc('day', logtime) > current_date + '7 day ago'::interval group > by to_char( logtime, 'Dy') ORDER BY to_char( logtime, 'D') DESC; > ERROR: column "sclog.logtime" must appear in the GROUP BY clause or be > used in an aggregate function > > Now obviously I don't want to group by logtime (a timestamp) so how do I > work around this? What I really need is a function that converts from > the char representation to a day of week number or vice versa. I also > have the same problem with month names.