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 3D7629DCA3A for ; Sat, 14 Jan 2006 09:13:38 -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 64495-09 for ; Sat, 14 Jan 2006 09:13:41 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id DFCC99DC9DA for ; Sat, 14 Jan 2006 09:13:35 -0400 (AST) Received: from ox.eicat.ca (ox.eicat.ca [66.96.30.35]) by svr4.postgresql.org (Postfix) with ESMTP id 576DF5AF023 for ; Sat, 14 Jan 2006 13:13:40 +0000 (GMT) Received: from dilbert (H245.C18.B96.tor.eicat.ca [66.96.18.245]) by ox.eicat.ca (Postfix) with SMTP id 66BBB1258F; Sat, 14 Jan 2006 08:13:37 -0500 (EST) Date: Sat, 14 Jan 2006 08:13:33 -0500 From: "D'Arcy J.M. Cain" To: Alan Chandler Cc: pgsql-sql@postgresql.org Subject: Re: Another orderby problem Message-Id: <20060114081333.3d1f223b.darcy@druid.net> In-Reply-To: <200601141302.49630.alan@chandlerfamily.org.uk> References: <200601141302.49630.alan@chandlerfamily.org.uk> X-Mailer: Sylpheed version 2.0.4 (GTK+ 2.8.9; i386--netbsdelf) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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/70 X-Sequence-Number: 23859 On Sat, 14 Jan 2006 13:02:48 +0000 Alan Chandler wrote: > select name, id, transaction.date as tdate, description, -amount as amount > from account left join transaction on name=src where name = 'Sarah' > union > select name, id, transaction.date as tdate, description, amount > from account join transaction on name=dst where name ='Sarah' > order by tdate asc; > > name | id | tdate | description | amount > -------+----+------------+---------------------------+-------- > Sarah | 12 | 0005-06-05 | Ben 21st Birthday Pressie | 41 > Sarah | 13 | 0005-06-05 | Cash from Mum | 60 > Sarah | 14 | 0005-06-08 | Petrol | 27.33 > Sarah | 15 | 0005-06-10 | Petrol Allowance | -40 > Sarah | 11 | 2005-06-05 | Sarah Petrol | 27.74 > (5 rows) > > I can't figure out why the dates are not in order (see transaction 11 is out > of place). It looks to be in order to me. Transaction 11 is 2,000 years later than the others. I suppose you are letting people enter two digit dates without running it through a cleaner process. If you put "05" as a year into a date field, PostgreSQL will take you at your word. if year < 50: year += 2000 elif year < 100: year += 1900 That will probably handle most cases for you. Alternatively, don't allow dates outside of your known domain. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/ | and a sheep voting on +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.