Solved

Using DECODE in a where clause

Posted on 2003-10-29
6
1,960 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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.

839 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