pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: C F <tacnaboyz@yahoo.com>
To: pgsql-sql@postgresql.org
Subject: Returning Multiple Values from CASE statement?
Date: Thu, 29 May 2003 09:05:56 -0700 (PDT)
Message-ID: <20030529160556.14465.qmail@web20413.mail.yahoo.com> (raw)

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).

Message-ID: <20030529160556.14465.qmail@web20413.mail.yahoo.com>
Permalink:  ../20030529160556.14465.qmail@web20413.mail.yahoo.com/
Also on:    postgresql.org/message-id/20030529160556.14465.qmail@web20413.mail.yahoo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tacnaboyz@yahoo.com
  Subject: Re: Returning Multiple Values from CASE statement?
  In-Reply-To: <20030529160556.14465.qmail@web20413.mail.yahoo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox