Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 5530F4765C7 for ; Fri, 6 Dec 2002 13:17:28 -0500 (EST) Received: from megazone.bigpanda.com (megazone.bigpanda.com [63.150.15.178]) by postgresql.org (Postfix) with ESMTP id CBD884760A9 for ; Fri, 6 Dec 2002 13:16:17 -0500 (EST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 2F611D610; Fri, 6 Dec 2002 10:16:17 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 250845C02; Fri, 6 Dec 2002 10:16:17 -0800 (PST) Date: Fri, 6 Dec 2002 10:16:17 -0800 (PST) From: Stephan Szabo To: FatTony Cc: Subject: Re: order by a "select as" determined by case statement In-Reply-To: <000001c29d52$131f4250$1401a8c0@nemesis> Message-ID: <20021206101155.K18245-100000@megazone23.bigpanda.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200212/78 X-Sequence-Number: 10666 On Fri, 6 Dec 2002, FatTony wrote: > 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? Probably what you want is: case when ... then ... else ... end as ticketage The aliases on the result columns of the subquery don't translate into the alias of the case expression, but you can give the case expression an alias instead.