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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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. |
| ||||
| 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. > > |