Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id B259D475E26 for ; Fri, 6 Dec 2002 13:03:26 -0500 (EST) Received: from smtp.comcast.net (smtp.comcast.net [24.153.64.2]) by postgresql.org (Postfix) with ESMTP id 35C21475A9E for ; Fri, 6 Dec 2002 13:03:26 -0500 (EST) Received: from nemesis (pcp02905416pcs.roylok01.mi.comcast.net [68.62.1.101]) by mtaout07.icomcast.net (iPlanet Messaging Server 5.2 HotFix 1.05 (built Nov 6 2002)) with ESMTP id <0H6P004GNM2FZR@mtaout07.icomcast.net> for pgsql-sql@postgresql.org; Fri, 06 Dec 2002 13:01:30 -0500 (EST) Date: Fri, 06 Dec 2002 13:05:34 -0500 From: FatTony Subject: order by a "select as" determined by case statement To: pgsql-sql@postgresql.org Message-id: <000001c29d52$131f4250$1401a8c0@nemesis> MIME-version: 1.0 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Mailer: Microsoft Outlook, Build 10.0.2627 Content-type: text/plain; charset=us-ascii Content-transfer-encoding: 7BIT Importance: Normal X-Priority: 3 (Normal) X-MSMail-priority: Normal X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200212/77 X-Sequence-Number: 10665 I'm not a db guru by any means so please forgive me if this has an easy solution. Scenario: Want to sort by an alias for SELECT AGE(). Problem is the timestamps for the SELECT AGE will be determined by the value of another column, thus the use of CASE. What I thought would work. SELECT tblticket.ticketid,tblstatus.statusname, CASE WHEN tblstatus.statusname = 'Closed' THEN (SELECT AGE(tblticket.ticketclosed,tblticket.ticketcreated) as ticketage) ELSE (SELECT AGE(CURRENT_TIMESTAMP,tblticket.ticketcreated) as ticketage) END FROM tblticket CROSS JOIN tblstatus WHERE (tblticket.reasonid = 2) AND blah, blah, blah ORDER BY ticketage ASC CASE apparently completely ignores the ticketage alias. Anyone know how to get around this? Thank You.