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 C07FC9DC850 for ; Tue, 24 Jan 2006 23:12:53 -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 32581-06 for ; Tue, 24 Jan 2006 23:12:58 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.tinet.org (mail.tinet.org [195.77.216.130]) by postgresql.org (Postfix) with ESMTP id E96009DC832 for ; Tue, 24 Jan 2006 23:12:49 -0400 (AST) Received: (qmail 16904 invoked from network); 25 Jan 2006 03:12:54 -0000 Received: from 135.red-81-38-24.dynamicip.rima-tde.net (HELO d505) (ipv@[81.38.24.135]) (envelope-sender ) by mail.tinet.org (qmail-ldap-1.03) with SMTP for ; 25 Jan 2006 03:12:54 -0000 Message-ID: <003a01c6215d$6a9320a0$0600000a@matt.mutua> Reply-To: From: To: References: Subject: Re: sorting by day of the week Date: Wed, 25 Jan 2006 04:14:12 +0100 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.67 required=5 tests=[AWL=0.120, NO_REAL_NAME=0.55] X-Spam-Score: 0.67 X-Spam-Level: X-Archive-Number: 200601/158 X-Sequence-Number: 23947 Hi, Try to use a calculated index: (tested similar solution, but not this code) CREATE OR REPLACE FUNCTION TestOrder (nameTable.weekDay%TYPE) RETURNS INT AS ' DECLARE numWeekDay INT; BEGIN if ($1 = ''Wed'') then numWeekDay := 1; if ($1 = ''Tue'') then numWeekDay := 2; ..... RETURN (numWeekDay); END; ' LANGUAGE 'plpgsql' STRICT IMMUTABLE; CREATE INDEX idx_TestOrder ON nameTable USING btree (TestOrder(nameTable.weekDay)); SELECT * FROM trajecte ORDER BY TestOrder(nameTable.weekDay); Regards ----- Original Message ----- From: "Joseph Shraibman" To: Sent: Wednesday, January 25, 2006 2:23 AM Subject: [SQL] sorting by day of the week > 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. > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org > >