Solved

Using DECODE in a where clause

Posted on 2003-10-29
6
1,962 Views
Last Modified: 2009-05-15
Using DECODE in where clause

I have a where clause that I want to be based on a parameter value. I could have different queries but I thought that I would try DECODE. The other challange is that the decode is in an IN clause. Here is the code.

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and CT.trip_grade in
(decode ('&GradeName','PreK','1',
'K','(1,2)',
'1','(''1'',''2'')',
'2','1,2',
'3','1,2,3',
'4','2,3',
'5','2,3',
'6','2,3,4',
'7','3,4',
'8','3,4',
'9','3,4',
'10','4',
'11','4',
'12','4','12345'));

This works fine if I enter PreK, 10, 11 or 12, but any other doesn't work. How can I introduce the list of options in the IN clause usint the DECODE THEN portion if I have more than one to return?

I tried two other approaches with 'K', and '1', but they don't work.

Angel

0
Comment
Question by:aleyva
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:andrewst
ID: 9643772
The problem is that "colname IN ('1,2,3')" doesn't have the same menaing as "colname IN (1,2,3)".  The first list has ONE varchar2 element '1,2,3' and the second has THREE elements 1, 2 and 3.

You could rewrite like this:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and (&GradeName = 1 and CT.trip_grade in (1,2)
      or &GradeName = 2 and CT.trip_grade in (1,2)
      or ... etc. etc.
      )

0
 
LVL 5

Expert Comment

by:FBIAGENT
ID: 9643901
or

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and substr(decode ('&GradeName','PreK','1',
'K','1,2',
'1','1,2',
'2','1,2',
'3','1,2,3',
'4','2,3',
'5','2,3',
'6','2,3,4',
'7','3,4',
'8','3,4',
'9','3,4',
'10','4',
'11','4',
'12','4','12345'),CT.trip_grade,1)>=1;
0
 

Author Comment

by:aleyva
ID: 9643907
What about something like "colname in ('1','2','3')"

How can I get DECODE to return that type of result?

Would the DECODE be faster or the OR?

A
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:andrewst
ID: 9643993
If trip_grade is a varchar2 then:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and (&GradeName = 1 and CT.trip_grade in ('1','2')
      or &GradeName = 2 and CT.trip_grade in ('1','2')
      or ... etc. etc.
      )

In your original post, you sometimes had (1,2) and sometimes had ('1','2'), which I found odd.  Column trip_grade is either a NUMBER or a VARCHAR2, it can't be both.

As for whether the DECODE or OR is faster (assuming you could get the DECODE to work), I don't suppose it makes any difference: both will cause a full table scan.


0
 
LVL 8

Accepted Solution

by:
Danielzt earned 500 total points
ID: 9643994
Can you try to create a table to save these values? this will make your coding and maintaining easy.

let's you use table grade_mapping
create table grade_mapping
( grade_name varchar2(20), grade number(2))

insert into table grade_mapping
valuse('Prek',1);
insert into table grade_mapping
valuse('K',1);
insert into table grade_mapping
valuse('K',2);
insert into table grade_mapping
valuse('1',1);
insert into table grade_mapping
valuse('1',2);
.....

So your query will be:

select CT.comp_triplet_id, CT.ach_comp_triplet
from compendix_triplet CT
where CT.subsubject_name = '&PrimarySubject'
and CT.trip_grade in
(select grade from grade_mapping where grade_name= '&GradeName')

0
 

Author Comment

by:aleyva
ID: 9644757
Well, the reason for the odd coding in my example is because I tried those three different approaches, sorry I wasn't clear on that.

What I am going to consider is the table mapping solution. This will make the maintance much easier because if the mapping changes, then all I have to do is change the data in the database, and not  have to change the Forms code.

Let's see how this works out for me.

A
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question