order by a "select as" determined by case statement

From: FatTony <fattony(at)comcast(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: order by a "select as" determined by case statement
Date: 2002-12-06 18:05:34
Message-ID: 000001c29d52$131f4250$1401a8c0@nemesis
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2002-12-06 18:16:17 Re: order by a "select as" determined by case statement
Previous Message Bruce Momjian 2002-12-06 17:41:07 Re: Rules/Trigges Trade-offs