Multiple return values in CASE statement?

This is a discussion on Multiple return values in CASE statement? within the MySQL Database forums, part of the Database Forums category; Hi, I've got a short question, hoping someone can point me to the right direction. I have a rather ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-27-2007
busnet
 
Posts: n/a
Default Multiple return values in CASE statement?

Hi,

I've got a short question, hoping someone can point me to the right
direction.
I have a rather complex SELECT statement with some CASE-query like

/-----
SELECT CASE
WHEN complexcalculations1 THEN returnvalue1
WHEN complexcalculations2 THEN returnvalue2
ELSE returnvalue3
END AS returnname
\-----

It works fine, but I want to have two different returnvalues for
whichever case is true.
I tried:

/-----
SELECT CASE
WHEN complexcalculations1 THEN returnvalue1a AS returnname_a,
returnvalue1b AS returnname_b
WHEN complexcalculations2 THEN returnvalue2a AS returnname_a,
returnvalue2b AS returnname_b
ELSE returnvalue3a AS returnname_a, returnvalue3b AS returnname_b
END
\-----

but that doesnt work. Of course I could use

/-----
SELECT CASE
WHEN complexcalculations1 THEN returnvalue1a
WHEN complexcalculations2 THEN returnvalue2a
ELSE returnvalue3a
END AS returnname_a
CASE
WHEN complexcalculations1 THEN returnvalue1b
WHEN complexcalculations2 THEN returnvalue2b
ELSE returnvalue3b
END AS returnname_b
\-----

But then all the complex calculations have to be done twice, which I
would call overhead.
Thanks for any ideas!

Reply With Quote
  #2 (permalink)  
Old 07-28-2007
petersprc
 
Posts: n/a
Default Re: Multiple return values in CASE statement?

I would repeat the expressions, as it's likely the db will optimize to
avoid duplicate calculations hopefully. You could store it in a string
variable and just build up your query.

You could also use a sub-query:

select case X when A then P when B then Q else R end case from (select
X, [...] as A, [...] as B from T)

On Jul 27, 5:06 pm, busnet <goo...@00l.de> wrote:
> Hi,
>
> I've got a short question, hoping someone can point me to the right
> direction.
> I have a rather complex SELECT statement with some CASE-query like
>
> /-----
> SELECT CASE
> WHEN complexcalculations1 THEN returnvalue1
> WHEN complexcalculations2 THEN returnvalue2
> ELSE returnvalue3
> END AS returnname
> \-----
>
> It works fine, but I want to have two different returnvalues for
> whichever case is true.
> I tried:
>
> /-----
> SELECT CASE
> WHEN complexcalculations1 THEN returnvalue1a AS returnname_a,
> returnvalue1b AS returnname_b
> WHEN complexcalculations2 THEN returnvalue2a AS returnname_a,
> returnvalue2b AS returnname_b
> ELSE returnvalue3a AS returnname_a, returnvalue3b AS returnname_b
> END
> \-----
>
> but that doesnt work. Of course I could use
>
> /-----
> SELECT CASE
> WHEN complexcalculations1 THEN returnvalue1a
> WHEN complexcalculations2 THEN returnvalue2a
> ELSE returnvalue3a
> END AS returnname_a
> CASE
> WHEN complexcalculations1 THEN returnvalue1b
> WHEN complexcalculations2 THEN returnvalue2b
> ELSE returnvalue3b
> END AS returnname_b
> \-----
>
> But then all the complex calculations have to be done twice, which I
> would call overhead.
> Thanks for any ideas!



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:28 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0