Unix Technical Forum

dynamically determining the field to select

This is a discussion on dynamically determining the field to select within the Oracle Database forums, part of the Database Server Software category; --> Hi SQL Masters, I'm trying to perform a rather simple query, but the field I'm selecting from the table ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-24-2008, 06:08 AM
hastenthunder
 
Posts: n/a
Default dynamically determining the field to select

Hi SQL Masters,

I'm trying to perform a rather simple query, but the field I'm selecting
from the table can only be determined at run time, basing on the value of
some other field in the same table. The logic is like:


IF (year_of_sales < 1995) THEN
SELECT fieldA
FROM myTable;
ELSE IF (year_of_sales >1995 and year_of_sales< 1998) THEN
SELECT fieldB
FROM myTable;
ELSE
SELECT fieldC
FROM myTable;
END IF;


The table looks like:

Create tabe myTable (year_of_sales number(4),
fieldA varchar2(5),
fieldB varcahr2(5),
fieldC varchar2(5));


Can someone help me to put the above query into a single SQL statement? Is
it possible?

Thanks a lot.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-24-2008, 06:08 AM
Peter Sylvester
 
Posts: n/a
Default Re: dynamically determining the field to select

Check out the "case" statement, I believe it can do this.
May also be able to use the "decode" function.

--Peter

hastenthunder wrote:
> Hi SQL Masters,
>
> I'm trying to perform a rather simple query, but the field I'm selecting
> from the table can only be determined at run time, basing on the value of
> some other field in the same table. The logic is like:
>
>
> IF (year_of_sales < 1995) THEN
> SELECT fieldA
> FROM myTable;
> ELSE IF (year_of_sales >1995 and year_of_sales< 1998) THEN
> SELECT fieldB
> FROM myTable;
> ELSE
> SELECT fieldC
> FROM myTable;
> END IF;
>
>
> The table looks like:
>
> Create tabe myTable (year_of_sales number(4),
> fieldA varchar2(5),
> fieldB varcahr2(5),
> fieldC varchar2(5));
>
>
> Can someone help me to put the above query into a single SQL statement? Is
> it possible?
>
> Thanks a lot.
>
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:57 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com