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 BB8F99DD735 for ; Sat, 14 Jan 2006 09:02:43 -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 66266-01 for ; Sat, 14 Jan 2006 09:02:45 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 06ACE9DD204 for ; Sat, 14 Jan 2006 09:02:40 -0400 (AST) Received: from home.chandlerfamily.org.uk (82-44-22-127.cable.ubr06.croy.blueyonder.co.uk [82.44.22.127]) by svr4.postgresql.org (Postfix) with ESMTP id 44AB25AF0C9 for ; Sat, 14 Jan 2006 13:02:43 +0000 (GMT) Received: from kanger.home ([192.168.0.21]) by home.chandlerfamily.org.uk with esmtp (Exim 4.50) id 1Exl3I-0003bR-R6 for pgsql-sql@postgresql.org; Sat, 14 Jan 2006 13:02:40 +0000 From: Alan Chandler To: pgsql-sql@postgresql.org Subject: Another orderby problem Date: Sat, 14 Jan 2006 13:02:48 +0000 User-Agent: KMail/1.9.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200601141302.49630.alan@chandlerfamily.org.uk> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200601/68 X-Sequence-Number: 23857 Just joined the list and have seen in the archive the thread on orderby. But mine seems different. I have a finanancial application with account and transactions which go between accounts (source and destination) but in which one either can be null. I am attempting to read an account and sort all the transactions by date. Here are the transactions id | date | src | dst | description | amount ----+------------+-------+-------+---------------------------+-------- 11 | 2005-06-05 | | Sarah | Sarah Petrol | 27.74 12 | 0005-06-05 | Sarah | | Ben 21st Birthday Pressie | -41 13 | 0005-06-05 | | Sarah | Cash from Mum | 60 14 | 0005-06-08 | | Sarah | Petrol | 27.33 15 | 0005-06-10 | | Sarah | Petrol Allowance | -40 This is the SQL 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). for reference the transaction table has the "date" field of type "date" -- Alan Chandler http://www.chandlerfamily.org.uk Open Source. It's the difference between trust and antitrust.