X-Original-To: pgsql-sql@postgresql.org Received: from localhost (developer.postgresql.org [64.117.224.193]) by developer.postgresql.org (Postfix) with ESMTP id 800DC925D38 for ; Thu, 29 May 2003 12:10:07 -0400 (EDT) Received: from developer.postgresql.org ([64.117.224.193]) by localhost (developer.postgresql.org [64.117.224.193:10024]) (amavisd-new) with ESMTP id 95806-09 for ; Thu, 29 May 2003 12:10:02 -0400 (EDT) Received: from web20413.mail.yahoo.com (web20401.mail.yahoo.com [66.163.169.89]) by developer.postgresql.org (Postfix) with SMTP id 93A0B925D36 for ; Thu, 29 May 2003 12:10:01 -0400 (EDT) Message-ID: <20030529160556.14465.qmail@web20413.mail.yahoo.com> Received: from [65.207.234.18] by web20401.mail.yahoo.com via HTTP; Thu, 29 May 2003 09:05:56 PDT Date: Thu, 29 May 2003 09:05:56 -0700 (PDT) From: C F Subject: Returning Multiple Values from CASE statement? To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1718942014-1054224356=:13098" X-Virus-Scanned: by amavisd-new X-Archive-Number: 200305/548 X-Sequence-Number: 13713 --0-1718942014-1054224356=:13098 Content-Type: text/plain; charset=us-ascii Hello, This is probably an odd request, but I have my reasons :) Basically, what I need to do is return *multiple* values each for a number of criteria. Imagine being able to return multiple values for each CASE statement... that's what I'm trying to do. I can solve this with subqueries, but as you'll see it's *really* ugly, and I'm sure there's a better way (performance is key). The other way obviously is to repeat the CASE statement for each value I want to return.... but that seems inefficient also. So I was wondering if someone could take a look at my query and slap me up side the head and set me straight? Here's the kind of query that would *like* to be able to do..... select (case when column1 = column2 then column5,column6) as alias5,alias6, (case when column3 = column4 then column7,column8) as alias7,alias8 from mytable ; and here's the only ugly way I know how to do it...... select a.*, (case a.alias2 = true then b.column5) as alias5, (case a.alias2 = true then b.column6) as alias6, (case a.alias4 = true then b.column7) as alias7, (case a.alias4 = true then b.column8) as alias8 from (select (case when column1 = column2 then true else false end) as alias2, (case when column3 = column4 then true else false end) as alias4 from mytable) a, mytable b where a.id = b.id ; Thanks!! --------------------------------- Do you Yahoo!? Free online calendar with sync to Outlook(TM). --0-1718942014-1054224356=:13098 Content-Type: text/html; charset=us-ascii
Hello,
This is probably an odd request, but I have my reasons :)  Basically, what I need to do is return *multiple* values each for a number of criteria.  Imagine being able to return multiple values for each CASE statement... that's what I'm trying to do.  I can solve this with subqueries, but as you'll see it's *really* ugly, and I'm sure there's a better way (performance is key).  The other way obviously is to repeat the CASE statement for each value I want to return.... but that seems inefficient also.  So I was wondering if someone could take a look at my query and slap me up side the head and set me straight?
 
Here's the kind of query that would *like* to be able to do.....
 
select
  (case when column1 = column2 then column5,column6) as alias5,alias6,
  (case when column3 = column4 then column7,column8) as alias7,alias8
from
  mytable
;
 
and here's the only ugly way I know how to do it......
 
select
  a.*,
  (case a.alias2 = true then b.column5) as alias5,
  (case a.alias2 = true then b.column6) as alias6,
  (case a.alias4 = true then b.column7) as alias7,
  (case a.alias4 = true then b.column8) as alias8
from
  (select
  (case when column1 = column2 then true else false end) as alias2,
  (case when column3 = column4 then true else false end) as alias4
  from
  mytable) a,
  mytable b
where
  a.id = b.id
;
Thanks!!


Do you Yahoo!?
Free online calendar with sync to Outlook(TM). --0-1718942014-1054224356=:13098--